Skip to content

Commit

Permalink
feat: add limited items per board
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgleam committed Oct 8, 2024
1 parent 080e4e0 commit 7a95836
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
44 changes: 34 additions & 10 deletions e2e/tests/board.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ test.beforeEach(async ({ page, boardId }) => {
await page.goto(`/boards/${boardId}`);
});

test.afterEach(async ({ page }) => {
test.afterEach(async ({ page, boardId }) => {
await page.goto(`/boards/${boardId}`);

const todoItems = page.getByTestId("todo-items").locator("ul > li");
await deleteItems(todoItems);
await deleteItems(todoItems, add1);

const doingItems = page.getByTestId("doing-items").locator("ul > li");
await deleteItems(doingItems);
await deleteItems(doingItems, identity);

const doneItems = page.getByTestId("done-items").locator("ul > li");
await deleteItems(doneItems);
await deleteItems(doneItems, identity);
});

async function deleteItems(locator: Locator) {
const add1: (number) => number = (a) => a + 1;

const identity: (number) => number = (a) => a;

async function deleteItems(locator: Locator, fn: (number) => number) {
const count = await locator.count();
if (count > 1) {
for (let i = 1; i < count; i++) {
await locator.nth(1).hover();
await locator.nth(1).getByTestId("delete-todo").click();
if (count > fn(0)) {
for (let i = fn(0); i < count; i++) {
await locator.nth(fn(0)).hover();
await locator.nth(fn(0)).getByTestId("delete-todo").click();
}
}
}
Expand Down Expand Up @@ -71,6 +77,24 @@ test.describe("New Todo", () => {
// Check that input is empty.
await expect(newTodo).toBeEmpty();
});

test("should not allow me to add todo items when the board is limited to the number of items", async ({ page }) => {
// create a new todo locator
const newTodo = page.getByPlaceholder("What needs to be done?");

for (let i = 0; i < 20; i++) {
await newTodo.fill(TODO_ITEMS[0]);
await page.getByTestId("create-todo").click();
}

const responseSubmit = page.waitForResponse('**/items/create');
// Create item todo.
await newTodo.fill(TODO_ITEMS[0]);
await page.getByTestId("create-todo").click();

const response = await responseSubmit;
expect(response.status()).toBe(400);
});
});

test.describe('Item', () => {
Expand Down Expand Up @@ -118,7 +142,7 @@ test.describe('Item', () => {
})
});

test.describe('Validate', () => {
test.describe('Invite', () => {
test('should allow me to add user', async ({ page, context }) => {
await page.getByTestId("show-menu").click();

Expand Down
29 changes: 29 additions & 0 deletions src/app/models/item.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,35 @@ pub fn patch_item(
Ok(count)
}

pub fn count_items(board_id: String, db: Connection) -> Int {
let sql =
"
SELECT
count(*)
FROM
tasks
WHERE
board_id = $1
"

let assert Ok(returned) =
pgo.execute(sql, db, [pgo.text(board_id)], dynamic.element(0, dynamic.int))

let assert [count] = returned.rows

count
}

pub fn validate_board_items(
board_id: String,
db: Connection,
) -> Result(Int, AppError) {
case count_items(board_id, db) {
a if a >= 20 -> Error(error.BadRequest)
a -> Ok(a)
}
}

pub fn item_status_to_string(status: ItemStatus) -> String {
case status {
Todo -> "TODO"
Expand Down
7 changes: 5 additions & 2 deletions src/app/routes/item_routes.gleam
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import app/error
import app/models/item.{create_item, delete_item, patch_item}
import app/models/item.{
create_item, delete_item, patch_item, validate_board_items,
}
import app/web.{type Context, Context}
import gleam/list
import gleam/result
import wisp.{type Request}

pub fn post_create_item(req: Request, ctx: Context, board_id: String) {
use form <- wisp.require_form(req)

let result = {
use item_content <- result.try(
list.key_find(form.values, "todo_input")
|> result.map_error(fn(_) { error.BadRequest }),
)
use _ <- result.try(validate_board_items(board_id, ctx.db))

create_item(board_id, item_content, ctx.db)
}

Expand Down

0 comments on commit 7a95836

Please sign in to comment.