Skip to content

Commit

Permalink
major refactoring to improve conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Dec 13, 2023
1 parent 7206179 commit f22959b
Show file tree
Hide file tree
Showing 24 changed files with 174 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use server";

import { createItem, getUserItemByName } from "@/data-access/items";
import { createItem } from "@/data-access/items/create-item.persistence";
import { getUserItemByName } from "@/data-access/items/get-items-by-name.persistence";
import { updateItem } from "@/data-access/items/update-item.persistence";
import { auth } from "@/lib/auth";
import { createItemUseCase } from "@/use-cases/items/create-item-use-case";
import { createItemUseCase } from "@/use-cases/items/create-item.use-case";
import { ValidationError } from "@/use-cases/items/utils";
import { revalidatePath } from "next/cache";

Expand Down Expand Up @@ -51,8 +53,9 @@ export async function createItemAction(
await createItemUseCase(
{
getUser,
createItem,
getUserItemByName,
createItem: createItem,
updateItem: updateItem,
getUserItemByName: getUserItemByName,
},
{
name: submittedForm.name.toLowerCase(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import { auth } from "@/lib/auth";
import { revalidatePath } from "next/cache";
import { updateItem, getItem, deleteItem } from "@/data-access/items";
import { decrementItemUseCase } from "@/use-cases/items/decrement-item-use-case";
import { decrementItemUseCase } from "@/use-cases/items/decrement-item.use-case";
import { deleteItem } from "@/data-access/items/delete-item.persistence";
import { getItem } from "@/data-access/items/get-item.persistence";
import { updateItem } from "@/data-access/items/update-item.persistence";

export type State = {
showToast: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use server";

import { deleteItem } from "@/data-access/items";
import { deleteItem } from "@/data-access/items/delete-item.persistence";
import { auth } from "@/lib/auth";
import { deleteItemUseCase } from "@/use-cases/items/delete-item-use-case";
import { deleteItemUseCase } from "@/use-cases/items/delete-item.use-case";
import { revalidatePath } from "next/cache";

export type DeleteFormState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import { auth } from "@/lib/auth";
import { revalidatePath } from "next/cache";
import { updateItem, getItem } from "@/data-access/items";
import { State } from "./decrement-item-action";
import { incrementItemUseCase } from "@/use-cases/items/increment-item-use-case";
import { State } from "./decrement-item.action";
import { incrementItemUseCase } from "@/use-cases/items/increment-item.use-case";
import { getItem } from "@/data-access/items/get-item.persistence";
import { updateItem } from "@/data-access/items/update-item.persistence";

export async function incrementItemAction(
state: State,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use server";

import { getItem, updateItem } from "@/data-access/items";
import { getItem } from "@/data-access/items/get-item.persistence";
import { updateItem } from "@/data-access/items/update-item.persistence";
import { auth } from "@/lib/auth";
import { markAsLowUseCase } from "@/use-cases/items/mark-as-low-use-case";
import { markAsLowUseCase } from "@/use-cases/items/mark-as-low.use-case";
import { revalidatePath } from "next/cache";

export type MarkLowState = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use server";

import { getItem, updateItem } from "@/data-access/items";
import { getItem } from "@/data-access/items/get-item.persistence";
import { updateItem } from "@/data-access/items/update-item.persistence";
import { auth } from "@/lib/auth";
import { unmarkAsLowUseCase } from "@/use-cases/items/unmark-as-low-use-case";
import { unmarkAsLowUseCase } from "@/use-cases/items/unmark-as-low.use-case";
import { revalidatePath } from "next/cache";

export type MarkLowState = {
Expand Down
2 changes: 1 addition & 1 deletion src/app/dashboard/create-item-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { HTMLAttributes, useEffect, useRef } from "react";
import { useToast } from "@/components/ui/use-toast";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Terminal } from "lucide-react";
import { createItemAction } from "./_actions/create-item-action";
import { createItemAction } from "./_actions/create-item.action";
import { cn } from "@/lib/utils";

export function CreateItemForm() {
Expand Down
10 changes: 5 additions & 5 deletions src/app/dashboard/items-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import {
TableHeader,
TableRow,
} from "@/components/ui/table";
import { deleteItemAction } from "./_actions/delete-item-action";
import { incrementItemAction } from "./_actions/increment-item-action";
import { decrementItemAction } from "./_actions/decrement-item-action";
import { deleteItemAction } from "./_actions/delete-item.action";
import { incrementItemAction } from "./_actions/increment-item.action";
import { decrementItemAction } from "./_actions/decrement-item.action";
import { useFormState } from "react-dom";
import { useToast } from "@/components/ui/use-toast";
import { markAsLowAction } from "./_actions/mark-as-low-action";
import { unmarkAsLowAction } from "./_actions/unmark-as-low-action";
import { markAsLowAction } from "./_actions/mark-as-low.action";
import { unmarkAsLowAction } from "./_actions/unmark-as-low.action";

export type Item = {
id: number;
Expand Down
2 changes: 1 addition & 1 deletion src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getItems } from "@/data-access/items";
import { CreateItemForm } from "./create-item-form";
import { ItemsTable } from "./items-table";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { getItems } from "@/data-access/items/get-items.persistence";
import { partition } from "lodash";
import { unstable_noStore } from "next/cache";

Expand Down
76 changes: 0 additions & 76 deletions src/data-access/items.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/data-access/items/create-item.persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "server-only";

import { db } from "@/db";
import { items } from "@/db/schema";

export type CreateItemDto = {
name: string;
userId: string;
quantity: number;
};

export async function createItem(item: CreateItemDto): Promise<void> {
await db.insert(items).values(item);
}
10 changes: 10 additions & 0 deletions src/data-access/items/delete-item.persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "server-only";

import { db } from "@/db";
import { items } from "@/db/schema";
import { eq } from "drizzle-orm";
import { ItemId } from "./get-item.persistence";

export async function deleteItem(itemId: ItemId): Promise<void> {
await db.delete(items).where(eq(items.id, itemId));
}
37 changes: 37 additions & 0 deletions src/data-access/items/get-item.persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import "server-only";

import { db } from "@/db";
import { Item, items } from "@/db/schema";
import { eq } from "drizzle-orm";

export type ItemId = number;

export type ItemDto = {
id: number;
name: string;
quantity: number;
userId: string;
isLow: boolean;
};

export function toDtoMapper(item: Item) {
return {
id: item.id,
name: item.name,
quantity: item.quantity,
userId: item.userId,
isLow: item.isLow,
};
}

export async function getItem(itemId: number): Promise<ItemDto> {
const foundItem = await db.query.items.findFirst({
where: eq(items.id, itemId),
});

if (!foundItem) {
throw new Error("could not find item");
}

return toDtoMapper(foundItem);
}
21 changes: 21 additions & 0 deletions src/data-access/items/get-items-by-name.persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "server-only";

import { db } from "@/db";
import { Item, items } from "@/db/schema";
import { eq, and } from "drizzle-orm";
import { ItemDto, toDtoMapper } from "./get-item.persistence";

export async function getUserItemByName(
userId: string,
name: string
): Promise<ItemDto | undefined> {
const foundItem = await db.query.items.findFirst({
where: and(eq(items.userId, userId), eq(items.name, name)),
});

if (!foundItem) {
return undefined;
}

return toDtoMapper(foundItem);
}
10 changes: 10 additions & 0 deletions src/data-access/items/get-items.persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "server-only";

import { db } from "@/db";
import { ItemDto, toDtoMapper } from "./get-item.persistence";

export async function getItems(): Promise<ItemDto[]> {
const items = await db.query.items.findMany();

return items.map(toDtoMapper);
}
10 changes: 10 additions & 0 deletions src/data-access/items/update-item.persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "server-only";

import { db } from "@/db";
import { items } from "@/db/schema";
import { eq } from "drizzle-orm";
import { ItemDto } from "./get-item.persistence";

export async function updateItem(item: ItemDto): Promise<void> {
await db.update(items).set(item).where(eq(items.id, item.id));
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { updateItem } from "@/data-access/items";
import { ItemEntity, ItemEntityValidationError } from "@/entites/item";
import {
GetUser,
CreateItem,
GetUserItemByName,
AuthenticationError,
itemToDto,
ValidationError,
itemToCreateItemDtoMapper,
} from "./utils";
import { CreateItem, GetUser, GetUserItemByName, UpdateItem } from "./types";

export async function createItemUseCase(
context: {
getUser: GetUser;
createItem: CreateItem;
updateItem: UpdateItem;
getUserItemByName: GetUserItemByName;
},
data: { name: string; quantity: number }
Expand All @@ -31,7 +29,7 @@ export async function createItemUseCase(
...existingItem,
quantity: existingItem.quantity + data.quantity,
});
await updateItem(itemToDto(updatedItem));
await context.updateItem(itemToDto(updatedItem));
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { ItemEntity } from "@/entites/item";
import {
GetUser,
UpdateItem,
GetItem,
AuthenticationError,
itemToDto,
DeleteItem,
} from "./utils";
import { AuthenticationError, itemToDto } from "./utils";
import { GetUser, DeleteItem, UpdateItem, GetItem } from "./types";

export async function decrementItemUseCase(
context: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GetUser, DeleteItem, AuthenticationError } from "./utils";
import { GetUser, DeleteItem } from "./types";
import { AuthenticationError } from "./utils";

export async function deleteItemUseCase(
context: { getUser: GetUser; deleteItem: DeleteItem },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { ItemDto } from "@/data-access/items";
import { ItemEntity } from "@/entites/item";
import {
AuthenticationError,
GetItem,
GetUser,
UpdateItem,
itemToDto,
} from "./utils";
import { AuthenticationError, itemToDto } from "./utils";
import { GetUser, UpdateItem, GetItem, ItemDto } from "./types";

export async function incrementItemUseCase(
context: { getUser: GetUser; updateItem: UpdateItem; getItem: GetItem },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { ItemEntity } from "@/entites/item";
import {
GetUser,
UpdateItem,
GetItem,
AuthenticationError,
itemToDto,
} from "./utils";
import { AuthenticationError, itemToDto } from "./utils";
import { GetUser, UpdateItem, GetItem } from "./types";

export async function markAsLowUseCase(
context: {
Expand Down
Loading

0 comments on commit f22959b

Please sign in to comment.