Skip to content
Snippets Groups Projects
Commit bdeae05b authored by Richard Fernando Heise Ferreira's avatar Richard Fernando Heise Ferreira
Browse files

Merge branch 'issue/38-create-achievements-table' into 'develop'

Issue #38/CREATE achievements table

See merge request !30
parents 7ca48512 d05f3177
Branches
No related tags found
1 merge request!30Issue #38/CREATE achievements table
CREATE TABLE IF NOT EXISTS "collection_likes" (
"user_id" integer NOT NULL,
"collection" integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "collection_resources" (
"collection_id" integer NOT NULL,
"resource_id" integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "collection_stats" (
"id" serial PRIMARY KEY NOT NULL,
"collection_id" bigint NOT NULL,
"views" bigint DEFAULT 0,
"downloads" bigint DEFAULT 0,
"likes" bigint DEFAULT 0,
"shares" bigint DEFAULT 0,
"score" bigint DEFAULT 0,
"follows" bigint DEFAULT 0,
CONSTRAINT "collection_stats_id_unique" UNIQUE("id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "collection" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(255),
"description" text,
"is_private" boolean,
"is_active" boolean DEFAULT true,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp,
"deleted_at" timestamp,
"thumbnail" varchar(255),
CONSTRAINT "collection_id_unique" UNIQUE("id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "complaint" (
"id" serial PRIMARY KEY NOT NULL,
"state" integer DEFAULT 0 NOT NULL,
"description" text NOT NULL,
"evaluatedUser" integer NOT NULL,
"resource_id" integer,
"collection_id" integer,
"user_id" integer,
"evaluated_at" timestamp,
"created_at" timestamp DEFAULT now() NOT NULL,
"q1" boolean,
"q2" boolean,
"q3" boolean,
"q4" boolean
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "educational_stages" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar NOT NULL,
CONSTRAINT "educational_stages_id_unique" UNIQUE("id"),
CONSTRAINT "educational_stages_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "follows" (
"id" serial PRIMARY KEY NOT NULL,
"user_id" integer NOT NULL,
"follower_id" integer NOT NULL,
CONSTRAINT "follows_id_unique" UNIQUE("id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "language" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar NOT NULL,
"code" varchar NOT NULL,
CONSTRAINT "language_id_unique" UNIQUE("id"),
CONSTRAINT "language_name_unique" UNIQUE("name"),
CONSTRAINT "language_code_unique" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "licenses" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar NOT NULL,
"description" varchar NOT NULL,
"url" varchar NOT NULL,
CONSTRAINT "licenses_id_unique" UNIQUE("id"),
CONSTRAINT "licenses_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "object_types" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(255) NOT NULL,
CONSTRAINT "object_types_id_unique" UNIQUE("id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "reset_ticket" (
"id" serial PRIMARY KEY NOT NULL,
"user_id" integer NOT NULL,
"token_hash" varchar(255) NOT NULL,
"expiration_date" timestamp NOT NULL,
"token_used" boolean DEFAULT false NOT NULL,
"valid_token" boolean DEFAULT true NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "reset_ticket_id_unique" UNIQUE("id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "resource_languages" (
"resource_id" integer NOT NULL,
"language_id" integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "resource_likes" (
"user_id" integer NOT NULL,
"resource_id" integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "resource" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(256) NOT NULL,
"author" varchar(256) NOT NULL,
"description" varchar(256),
"bucket_key" varchar(256),
"link" varchar(256),
"thumbnail" varchar(256) NOT NULL,
"active" boolean DEFAULT false NOT NULL,
"published_at" timestamp,
"submited_at" timestamp,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp,
"deleted_at" timestamp,
CONSTRAINT "resource_id_unique" UNIQUE("id"),
CONSTRAINT "resource_bucket_key_unique" UNIQUE("bucket_key")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "role" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(255) NOT NULL,
"description" text,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "role_id_unique" UNIQUE("id"),
CONSTRAINT "role_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "stats_resources" (
"id" serial PRIMARY KEY NOT NULL,
"resource_id" bigint NOT NULL,
"views" bigint DEFAULT 0 NOT NULL,
"downloads" bigint DEFAULT 0 NOT NULL,
"shares" bigint DEFAULT 0 NOT NULL,
"score" bigint DEFAULT 0 NOT NULL,
CONSTRAINT "stats_resources_id_unique" UNIQUE("id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "subjects" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar NOT NULL,
CONSTRAINT "subjects_id_unique" UNIQUE("id"),
CONSTRAINT "subjects_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "submission" (
"id" serial PRIMARY KEY NOT NULL,
"is_accepted" boolean DEFAULT false NOT NULL,
"justification" text,
"resource_id" integer NOT NULL,
"submitter_id" integer NOT NULL,
"curator_id" integer NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp,
"answered_at" timestamp,
"q1" boolean,
"q2" boolean,
"q3" boolean,
"q4" boolean
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user_role" (
"id" serial PRIMARY KEY NOT NULL,
"user_id" integer NOT NULL,
"role_id" integer NOT NULL,
CONSTRAINT "user_role_id_unique" UNIQUE("id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user_stats" (
"id" serial PRIMARY KEY NOT NULL,
"user_id" integer NOT NULL,
"score" integer DEFAULT 0 NOT NULL,
"likes" integer DEFAULT 0 NOT NULL,
"likes_received" integer DEFAULT 0 NOT NULL,
"follows" integer DEFAULT 0 NOT NULL,
"followers" integer DEFAULT 0 NOT NULL,
"collections" integer DEFAULT 0 NOT NULL,
"submitted_resources" integer DEFAULT 0 NOT NULL,
"approved_resources" integer DEFAULT 0 NOT NULL,
"reviewed_resources" integer DEFAULT 0 NOT NULL,
"comments" integer DEFAULT 0 NOT NULL,
CONSTRAINT "user_stats_id_unique" UNIQUE("id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(255) NOT NULL,
"username" varchar(255) NOT NULL,
"password" varchar(255) NOT NULL,
"email" varchar(255) NOT NULL,
"description" text DEFAULT 'sem descrição',
"institution" text DEFAULT 'sem instituição',
"birthday" timestamp NOT NULL,
"cpf" varchar(255) NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
"confirmed_at" timestamp,
"confirmation_sent_at" timestamp,
"deleted_at" timestamp,
"reactivated_at" timestamp,
"active" boolean DEFAULT true,
CONSTRAINT "user_id_unique" UNIQUE("id"),
CONSTRAINT "user_username_unique" UNIQUE("username"),
CONSTRAINT "user_email_unique" UNIQUE("email")
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "collection_likes" ADD CONSTRAINT "collection_likes_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "collection_likes" ADD CONSTRAINT "collection_likes_collection_collection_id_fk" FOREIGN KEY ("collection") REFERENCES "public"."collection"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "collection_resources" ADD CONSTRAINT "collection_resources_collection_id_collection_id_fk" FOREIGN KEY ("collection_id") REFERENCES "public"."collection"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "collection_resources" ADD CONSTRAINT "collection_resources_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "collection_stats" ADD CONSTRAINT "collection_stats_collection_id_collection_id_fk" FOREIGN KEY ("collection_id") REFERENCES "public"."collection"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "complaint" ADD CONSTRAINT "complaint_evaluatedUser_user_id_fk" FOREIGN KEY ("evaluatedUser") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "follows" ADD CONSTRAINT "follows_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "follows" ADD CONSTRAINT "follows_follower_id_user_id_fk" FOREIGN KEY ("follower_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "reset_ticket" ADD CONSTRAINT "reset_ticket_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "resource_languages" ADD CONSTRAINT "resource_languages_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "resource_languages" ADD CONSTRAINT "resource_languages_language_id_language_id_fk" FOREIGN KEY ("language_id") REFERENCES "public"."language"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "resource_likes" ADD CONSTRAINT "resource_likes_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "resource_likes" ADD CONSTRAINT "resource_likes_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "stats_resources" ADD CONSTRAINT "stats_resources_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "submission" ADD CONSTRAINT "submission_resource_id_resource_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resource"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "submission" ADD CONSTRAINT "submission_submitter_id_user_id_fk" FOREIGN KEY ("submitter_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "submission" ADD CONSTRAINT "submission_curator_id_user_id_fk" FOREIGN KEY ("curator_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "user_role" ADD CONSTRAINT "user_role_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "user_role" ADD CONSTRAINT "user_role_role_id_role_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."role"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "user_stats" ADD CONSTRAINT "user_stats_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
This diff is collapsed.
...@@ -5,15 +5,8 @@ ...@@ -5,15 +5,8 @@
{ {
"idx": 0, "idx": 0,
"version": "7", "version": "7",
"when": 1729264247365, "when": 1729522512009,
"tag": "0000_lyrical_silver_samurai", "tag": "0000_narrow_payback",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1729512790298,
"tag": "0001_eager_living_lightning",
"breakpoints": true "breakpoints": true
} }
......
import { Service } from "typedi";
import type { AchievementInput, AchievementModel, AchievementUpdate } from "../schema/achievements.schema";
import db from "..";
import achievementTable, { achievementSchemas } from "../schema/achievements.schema";
import { eq } from "drizzle-orm";
@Service()
export class AchievementRepo {
async create(achievement: AchievementInput): Promise<AchievementModel> {
const [ret] = await db
.insert(achievementTable)
.values(achievement)
.returning()
return achievementSchemas.achievementModelSchema.parse(ret)
}
async update(achievement: AchievementUpdate): Promise<AchievementModel> {
achievement.updated_at = new Date().toISOString()
const [ret] = await db
.update(achievementTable)
.set(achievement)
.where(eq(achievementTable.id, achievement.id))
.returning()
return achievementSchemas.achievementModelSchema.parse(ret)
}
async delete(id: AchievementModel['id']): Promise<AchievementModel> {
const [ret] = await db
.delete(achievementTable)
.where(eq(achievementTable.id, id))
.returning()
return achievementSchemas.achievementModelSchema.parse(ret)
}
async find(
id: AchievementModel['id']
): Promise<AchievementModel | undefined> {
const achievement = await db.query.achievementTable.findFirst({
where: eq(achievementTable.id, id)
})
if(!achievement) return undefined
return achievementSchemas.achievementModelSchema.parse(achievement)
}
async findMany(): Promise<AchievementModel[]> {
return achievementSchemas.achievementModelSchema
.array()
.parse(await db.query.achievementTable.findMany())
}
}
\ No newline at end of file
import { boolean, integer, numeric, pgEnum, pgTable, serial, text, timestamp, varchar } from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
const stateEnum = pgEnum('state', ['active', 'inactive', 'under_review'])
const achievementTable = pgTable('achievement', {
id: serial('id').primaryKey().notNull().unique(),
name: varchar('name', { length: 255 }).notNull(),
description: text('description'),
reward_experience: numeric('reward_experience'),
reward_points: numeric('reward_points'),
// o campo state foi comentado pois a criação do tipo enum terá que ser feita manualmente na migração
//state: stateEnum('state').notNull().default('inactive'),
state: integer('state').notNull().default(0),
repeatable: integer('repeatable').notNull(),
is_resettable: boolean('is_resettable').notNull(),
created_at: timestamp('created_at', { mode: 'string' }).notNull().defaultNow(),
updated_at: timestamp('updated_at', { mode: 'string' }).notNull().defaultNow(),
})
const achievementModelSchema = createSelectSchema(achievementTable)
const achievementDtoSchema = achievementModelSchema
const achievementInputSchema = createInsertSchema(achievementTable)
const achievementUpdateSchema = achievementInputSchema.partial().required({ id: true })
export type AchievementModel = z.infer<typeof achievementModelSchema>
export type AchievementDto = z.infer<typeof achievementDtoSchema>
export type AchievementInput = z.infer<typeof achievementInputSchema>
export type AchievementUpdate = z.infer<typeof achievementUpdateSchema>
export const achievementSchemas = {
achievementModelSchema,
achievementDtoSchema,
achievementInputSchema,
achievementUpdateSchema,
}
export default achievementTable
\ No newline at end of file
...@@ -15,6 +15,7 @@ import userTable from './user.schema' ...@@ -15,6 +15,7 @@ import userTable from './user.schema'
import objectTypeTable from './object-type.schema' import objectTypeTable from './object-type.schema'
import complaintTable from './complaints.schema' import complaintTable from './complaints.schema'
import resourceSubjectsTable from './resource-subjects.schema' import resourceSubjectsTable from './resource-subjects.schema'
import achievementTable from './achievements.schema'
import resourceLanguagesTable from './resource-languages.schema' import resourceLanguagesTable from './resource-languages.schema'
import followRelationTable from '../relations/follows.relation' import followRelationTable from '../relations/follows.relation'
...@@ -53,7 +54,8 @@ export { ...@@ -53,7 +54,8 @@ export {
institutionTable, institutionTable,
userInstitutionRelationTable, userInstitutionRelationTable,
resourceLikesTable, resourceLikesTable,
resourceEducationalStagesTable resourceEducationalStagesTable,
achievementTable,
} }
export const tables = [ export const tables = [
...@@ -79,4 +81,5 @@ export const tables = [ ...@@ -79,4 +81,5 @@ export const tables = [
institutionTable, institutionTable,
userInstitutionRelationTable, userInstitutionRelationTable,
resourceLikesTable, resourceLikesTable,
achievementTable,
] ]
...@@ -44,5 +44,6 @@ await seeds.resourceSubjectsSeed(db) ...@@ -44,5 +44,6 @@ await seeds.resourceSubjectsSeed(db)
await seeds.userInstitutionSeed(db) await seeds.userInstitutionSeed(db)
await seeds.resourceLikesSeed(db) await seeds.resourceLikesSeed(db)
await seeds.resourceEducationalStagesSeed(db) await seeds.resourceEducationalStagesSeed(db)
await seeds.achievementSeed(db)
await connection.end() await connection.end()
import type db from "..";
import { achievementTable } from "../schema";
import type { AchievementInput } from "../schema/achievements.schema";
export default async function seed(db: db) {
await db.insert(achievementTable).values(achievementData)
}
const achievementData: AchievementInput[] = [
{
name: 'Achievement 1',
description: 'Achievement 1 description',
reward_experience: 100,
reward_points: 10,
state: 2,
repeatable: 5,
is_resettable: true,
},
{
name: 'Achievement 2',
description: 'Achievement 2 description',
reward_experience: 200,
reward_points: 20,
state: 1,
repeatable: 5,
is_resettable: true,
},
{
name: 'Achievement 3',
description: 'Achievement 3 description',
reward_experience: 300,
reward_points: 30,
state: 1,
repeatable: 5,
is_resettable: true,
}
]
\ No newline at end of file
...@@ -21,3 +21,4 @@ export { default as institutionSeed } from './institutions.seed' ...@@ -21,3 +21,4 @@ export { default as institutionSeed } from './institutions.seed'
export { default as userInstitutionSeed } from './user-institution.seed' export { default as userInstitutionSeed } from './user-institution.seed'
export { default as resourceLikesSeed } from './resource-likes.seed' export { default as resourceLikesSeed } from './resource-likes.seed'
export { default as resourceEducationalStagesSeed } from './resource-educational-stages.seed' export { default as resourceEducationalStagesSeed } from './resource-educational-stages.seed'
export { default as achievementSeed } from './achievement.seed'
...@@ -33,6 +33,7 @@ import { publicResourceSubjectsRouter, resourceSubjectsRouter } from './routes/r ...@@ -33,6 +33,7 @@ import { publicResourceSubjectsRouter, resourceSubjectsRouter } from './routes/r
import { userInstitutionRouter } from './routes/user-institution.route' import { userInstitutionRouter } from './routes/user-institution.route'
import { publicResourceLikesRoutes, resourceLikesRoutes } from './routes/resource-likes.route' import { publicResourceLikesRoutes, resourceLikesRoutes } from './routes/resource-likes.route'
import { publicResourceEducationalStagesRouter, resourceEducationalStagesRouter } from './routes/resource-educational-stages.route' import { publicResourceEducationalStagesRouter, resourceEducationalStagesRouter } from './routes/resource-educational-stages.route'
import { achievementRouter, publicAchievementRouter } from './routes/achievement.route'
const app = new Hono() const app = new Hono()
...@@ -89,6 +90,7 @@ app ...@@ -89,6 +90,7 @@ app
.route('/resourceEducationalStages', publicResourceEducationalStagesRouter) .route('/resourceEducationalStages', publicResourceEducationalStagesRouter)
.route('/resourceLikes', publicResourceLikesRoutes) .route('/resourceLikes', publicResourceLikesRoutes)
.route('/achievements', publicAchievementRouter)
//rotas que precisam de token //rotas que precisam de token
app app
.basePath('/api') .basePath('/api')
...@@ -116,6 +118,7 @@ app ...@@ -116,6 +118,7 @@ app
.route('/user-institutions', userInstitutionRouter) .route('/user-institutions', userInstitutionRouter)
.route('/resourceEducationalStages', resourceEducationalStagesRouter) .route('/resourceEducationalStages', resourceEducationalStagesRouter)
.route('/achievements', achievementRouter)
.route('/resourceLikes', resourceLikesRoutes) .route('/resourceLikes', resourceLikesRoutes)
export default app export default app
export type AppType = typeof app export type AppType = typeof app
......
import { AchievementService } from "@/services/achievement.service";
import Container, { ContainerInstance } from "typedi";
import { honoWithJwt } from "..";
import { zValidator } from "@hono/zod-validator";
import { achievementSchemas } from "@/db/schema/achievements.schema";
import { createApexError, HttpStatus } from "@/services/error.service";
import { Hono } from "hono";
const service = Container.get(AchievementService)
export const achievementRouter = honoWithJwt()
.post('/create', zValidator('json', achievementSchemas.achievementInputSchema),
async (c) => {
try {
const input = await c.req.valid('json')
const achievement = achievementSchemas.achievementDtoSchema.parse(
await service.create(input)
)
return c.json({achievement})
} catch (e) {
return c.json(
createApexError({
status: 'error',
message: 'could not create achievement',
code: HttpStatus.BAD_REQUEST,
path: c.req.routePath,
suggestion: 'check the input and try again',
}),
HttpStatus.BAD_REQUEST
)
}
}
)
.post('/update', zValidator('json', achievementSchemas.achievementUpdateSchema),
async (c) => {
try {
const input = await c.req.valid('json')
const achievement = achievementSchemas.achievementDtoSchema.parse(
await service.update(input)
)
return c.json({achievement})
} catch (e) {
console.log(e)
return c.json(
createApexError({
status: 'error',
message: 'could not update achievement',
code: HttpStatus.BAD_REQUEST,
path: c.req.routePath,
suggestion: 'check the input and try again',
}),
HttpStatus.BAD_REQUEST
)
}
}
)
.post('/delete/:id', async (c) => {
try {
const id = +c.req.param('id')
const achievement = achievementSchemas.achievementDtoSchema.parse(
await service.delete(id)
)
return c.json({achievement})
} catch (e) {
return c.json(
createApexError({
status: 'error',
message: 'could not delete achievement',
code: HttpStatus.BAD_REQUEST,
path: c.req.routePath,
suggestion: 'check the input and try again',
}),
HttpStatus.BAD_REQUEST
)
}
})
export const publicAchievementRouter = new Hono()
.get('/:id', async (c) => {
try {
const id = +c.req.param('id')
const achievement = achievementSchemas.achievementDtoSchema.parse(
await service.find(id)
)
return c.json({achievement})
} catch (e) {
return c.json(
createApexError({
status: 'error',
message: 'could not find achievement',
code: HttpStatus.NOT_FOUND,
path: c.req.routePath,
suggestion: 'check the input and try again',
}),
HttpStatus.NOT_FOUND
)
}
})
.get('/', async (c) => {
try {
const achievements = achievementSchemas.achievementDtoSchema.array().parse(
await service.findMany()
)
return c.json({achievements})
} catch (e) {
return c.json(
createApexError({
status: 'error',
message: 'could not find achievements',
code: HttpStatus.NOT_FOUND,
path: c.req.routePath,
suggestion: 'check the input and try again',
}),
HttpStatus.NOT_FOUND
)
}
})
\ No newline at end of file
import { AchievementRepo } from "@/db/repo/achievement.repo";
import type { AchievementInput, AchievementModel, AchievementUpdate } from "@/db/schema/achievements.schema";
import { Inject, Service } from "typedi";
@Service()
export class AchievementService {
@Inject()
private readonly repo: AchievementRepo;
async create(achievement: AchievementInput): Promise<AchievementModel> {
return this.repo.create(achievement)
}
async update(achievement: AchievementUpdate): Promise<AchievementModel> {
return this.repo.update(achievement)
}
async delete(id: AchievementModel['id']): Promise<AchievementModel> {
return this.repo.delete(id)
}
async find(id: AchievementModel['id']): Promise<AchievementModel | undefined> {
return this.repo.find(id)
}
async findMany(): Promise<AchievementModel[]> {
return this.repo.findMany()
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment