Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add model apis #29

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Warnings:

- You are about to drop the `Category` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropForeignKey
ALTER TABLE "DefaultModel" DROP CONSTRAINT "DefaultModel_category_id_fkey";

-- AlterTable
ALTER TABLE "VerificationEmail" ALTER COLUMN "expiration_date" SET DEFAULT NOW() + interval '1 day';

-- DropTable
DROP TABLE "Category";
9 changes: 1 addition & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ model DefaultModel {
model_id String @id
model Model @relation(fields: [model_id], references: [id])
category_id String
category Category @relation(fields: [category_id], references: [id])
likesNo Int
imageUrl String @db.VarChar(255)
subImageUrls String[] @db.VarChar(255)
subImageUrls String[] @db.VarChar(255)
Like Like[]
}

Expand Down Expand Up @@ -135,12 +134,6 @@ model Item {
@@id([model_id, order_id])
}

model Category {
id String @id @default(cuid())
name String @db.VarChar(50)
DefaultModel DefaultModel[]
}

enum UserRole {
CUSTOMER
MANAGER
Expand Down
27 changes: 24 additions & 3 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const models = [
];

async function handleUsers() {
return users.map((user) => {
const _handleUsers = users.map((user) => {
const hashPassword = hashSync(user.password, SALT_ROUNDS);
return {
email: user.email,
Expand All @@ -127,6 +127,29 @@ async function handleUsers() {
verified: user?.verified
};
});

_handleUsers.forEach(async (user) => {
const { role, id } =
(await prisma.user.create({
data: user
})) || {};

if (role === UserRole.CUSTOMER) {
await prisma.customer.create({
data: {
user_id: id
}
});
} else {
await prisma.manager.create({
data: {
user_id: id
}
});
}
});

return _handleUsers;
}

async function handleCategories() {
Expand Down Expand Up @@ -179,8 +202,6 @@ async function generateSampleData() {

const handlePromotionResult = await handlePromotions();
console.log(await prisma.modelPromotion.createMany({ data: handlePromotionResult }));

process.exit(0);
}

generateSampleData();
3 changes: 3 additions & 0 deletions src/constants/errorMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ export const INVALID_TOKEN = 'Invalid token !';
export const NOT_FOUND_GENERIC = 'Not found !';
export const INVALID_VERIFICATION_LINK = 'Invalid or expired verification link !';
export const ADD_CART_FAILED = 'Failed to add some models';
export const MODEL_NOT_FOUND = 'This model does not exist';
export const DELETE_MODEL_FAILED = 'Model deletion failed';
export const UPDATE_MODEL_FAILED = 'Model update failed';

export const PERMISSION_DENIED = 'You do not have permission to access this resource.';
4 changes: 4 additions & 0 deletions src/dtos/in/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export * from './auth.dto';
export * from './order.dto';
export * from './paypal.dto';
export * from './register.dto';
export * from './updateDefaultModel.dto';
export * from './updateUserModel.dto';
export * from './uploadDefaultModel.dto';
export * from './uploadUserModel.dto';
13 changes: 13 additions & 0 deletions src/dtos/in/updateDefaultModel.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Static, Type } from '@sinclair/typebox';

// See https://github.com/sinclairzx81/typebox

export const UpdateDefaultModelInputDto = Type.Object({
name: Type.Optional(Type.String()),
price: Type.Optional(Type.Number()),
gcode: Type.Optional(Type.String()),
imageUrl: Type.Optional(Type.String()),
category_id: Type.Optional(Type.String())
});

export type UpdateDefaultModelInputDto = Static<typeof UpdateDefaultModelInputDto>;
11 changes: 11 additions & 0 deletions src/dtos/in/updateUserModel.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Static, Type } from '@sinclair/typebox';

// See https://github.com/sinclairzx81/typebox

export const UpdateUserModelInputDto = Type.Object({
name: Type.Optional(Type.String()),
price: Type.Optional(Type.Number()),
gcode: Type.Optional(Type.String())
});

export type UpdateUserModelInputDto = Static<typeof UpdateUserModelInputDto>;
15 changes: 15 additions & 0 deletions src/dtos/in/uploadDefaultModel.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Static, Type } from '@sinclair/typebox';

// See https://github.com/sinclairzx81/typebox

export const UploadDefaultModelInputDto = Type.Array(
Type.Object({
name: Type.String(),
price: Type.Number(),
gcode: Type.String(),
imageUrl: Type.String(),
category_id: Type.String()
})
);

export type UploadDefaultModelInputDto = Static<typeof UploadDefaultModelInputDto>;
12 changes: 12 additions & 0 deletions src/dtos/in/uploadUserModel.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Static, Type } from '@sinclair/typebox';

// See https://github.com/sinclairzx81/typebox

export const UploadUserModelInputDto = Type.Array(
Type.Object({
name: Type.String(),
gcode: Type.String()
})
);

export type UploadUserModelInputDto = Static<typeof UploadUserModelInputDto>;
16 changes: 16 additions & 0 deletions src/dtos/out/defaultModel.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Static, Type } from '@sinclair/typebox';

// See https://github.com/sinclairzx81/typebox

export const DefaultModelResultDto = Type.Object({
id: Type.String(),
name: Type.String(),
price: Type.Number(),
category_id: Type.String(),
imageUrl: Type.String(),
likesNo: Type.Number(),
gcode: Type.String(),
uploadTime: Type.String({ format: 'date-time' })
});

export type DefaultModelResultDto = Static<typeof DefaultModelResultDto>;
16 changes: 16 additions & 0 deletions src/dtos/out/defaultModelList.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Static, Type } from '@sinclair/typebox';

// See https://github.com/sinclairzx81/typebox

export const DefaultModelListResultDto = Type.Array(
Type.Object({
id: Type.String(),
name: Type.String(),
price: Type.Number(),
imageUrl: Type.String(),
likesNo: Type.Number(),
uploadTime: Type.String({ format: 'date-time' })
})
);

export type DefaultModelListResultDto = Static<typeof DefaultModelListResultDto>;
4 changes: 4 additions & 0 deletions src/dtos/out/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
*/

export * from './auth.dto';
export * from './defaultModel.dto';
export * from './defaultModelList.dto';
export * from './getCart.dto';
export * from './home.dtos';
export * from './model.dto';
export * from './order.dto';
export * from './paypal.dto';
export * from './register.dto';
export * from './user.dto';
export * from './userModel.dto';
export * from './userModelList.dto';
13 changes: 13 additions & 0 deletions src/dtos/out/userModel.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Static, Type } from '@sinclair/typebox';

// See https://github.com/sinclairzx81/typebox

export const UserModelResultDto = Type.Object({
id: Type.String(),
name: Type.String(),
price: Type.Number(),
gcode: Type.String(),
uploadTime: Type.String({ format: 'date-time' })
});

export type UserModelResultDto = Static<typeof UserModelResultDto>;
14 changes: 14 additions & 0 deletions src/dtos/out/userModelList.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Static, Type } from '@sinclair/typebox';

// See https://github.com/sinclairzx81/typebox

export const UserModelListResultDto = Type.Array(
Type.Object({
id: Type.String(),
name: Type.String(),
price: Type.Number(),
uploadTime: Type.String({ format: 'date-time' })
})
);

export type UserModelListResultDto = Static<typeof UserModelListResultDto>;
Loading
Loading