11import { Context , Hono } from "jsr:@hono/hono" ;
22import { cors } from 'jsr:@hono/hono/cors' ;
3- import { AccountStatusValue , ACCOUNT_STATUS_VALUES } from "@shared/mod.ts" ;
3+ import { AccountStatusValue , ACCOUNT_STATUS_VALUES , baseTagSchema } from "@shared/mod.ts" ;
44import { createCourse } from "./controller/create_course_activity.ts" ;
55import { getCourse } from "./controller/get_course_activity.ts" ;
66import { addUserToCourse } from "./controller/add_course_member_activity.ts" ;
@@ -26,6 +26,12 @@ import {
2626 DefaultRoles ,
2727 Permissions
2828} from '../../../../shared/schema/course.ts' ;
29+ import { getAllTags } from "./controller/get_all_tags_activity.ts" ;
30+ import { NestedTag , Tag , tagPermissionsSchema , updateTagSchema } from "@shared/schema/tag.ts" ;
31+ import { getTag , getTagNested } from "../_shared/utils/tag_helper.ts" ;
32+ import { createTag } from "./controller/create_tag_activity.ts" ;
33+ import { updateTag } from "./controller/update_tag_activity.ts" ;
34+ import { deleteTag } from "./controller/delete_tag_activity.ts" ;
2935
3036const functionName = "courses" ;
3137const app = new Hono ( ) . basePath ( `/${ functionName } ` ) ;
@@ -148,7 +154,7 @@ app.put("/:course_id", async (c: Context<{ Variables: UserVariables}>) => {
148154// Add user to course
149155app . post ( "/:course_id/members/:user_id" , async ( c : Context < { Variables : UserVariables } > ) => {
150156 try {
151- const course_id = c . req . param ( "course_id" ) ;
157+ const course_id = c . req . param ( "course_id" ) ;
152158 const user_id = c . req . param ( "user_id" ) ;
153159 const caller = c . var . user ;
154160 let role = ( await c . req . json ( ) ) . role ;
@@ -233,5 +239,137 @@ app.get("/:course_id/members", async (c: Context<{ Variables: UserVariables}>) =
233239 }
234240} ) ;
235241
242+ // Get all tags of course
243+ app . get ( "/:course_id/tags" , async ( c : Context < { Variables : UserVariables } > ) => {
244+ try {
245+ const course_id = c . req . param ( "course_id" ) ;
246+ const user = c . var . user ;
247+
248+ if ( ! ( await isUserMemberOfCourse ( user . id , course_id ) ) ) {
249+ return c . json ( { error : "Unauthorized" } , 401 ) ;
250+ }
251+
252+ const tags = await getAllTags ( course_id ) ;
253+ return c . json ( { tags : tags } , 200 ) ;
254+ } catch ( error ) {
255+ console . error ( error ) ;
256+ return c . json ( { error : "Internal server error" } , 500 ) ;
257+ }
258+ } ) ;
259+
260+ // Get tag
261+ app . get ( "/:course_id/tags/:tag_id" , async ( c : Context < { Variables : UserVariables } > ) => {
262+ try {
263+ const { course_id, tag_id } = c . req . param ( ) ;
264+ const nested = c . req . query ( "nested" ) ;
265+ const user = c . var . user ;
266+
267+ if ( ! ( await isUserMemberOfCourse ( user . id , course_id ) ) ) {
268+ return c . json ( { error : "Unauthorized" } , 401 ) ;
269+ }
270+
271+ const tag : Tag | NestedTag = nested ? await getTagNested ( tag_id ) : await getTag ( tag_id ) ;
272+ return c . json ( { tag : tag } , 200 ) ;
273+ } catch ( error ) {
274+ console . error ( error ) ;
275+ if ( error instanceof NotFoundError ) {
276+ return c . json ( { error : error . message } , 404 ) ;
277+ }
278+ return c . json ( { error : "Internal server error" } , 500 ) ;
279+ }
280+ } ) ;
281+
282+ // Create a tag
283+ app . post ( "/:course_id/tags" , async ( c : Context < { Variables : UserVariables } > ) => {
284+ try {
285+ const course_id = c . req . param ( "course_id" ) ;
286+ const user = c . var . user ;
287+ const newTagData = await c . req . json ( ) ;
288+
289+ if ( ! newTagData . permissions ) {
290+ newTagData . permissions = tagPermissionsSchema . parse ( { } ) ;
291+ }
292+
293+ const validationResult = baseTagSchema . safeParse ( newTagData ) ;
294+ if ( ! validationResult . success ) {
295+ return c . json ( {
296+ error : "Validation failed" ,
297+ details : validationResult . error . errors ,
298+ } , 400 ) ;
299+ }
300+
301+ const completeNewTagData = validationResult . data ;
302+
303+ const permissions = await getUserPermissionsInCourse ( user . id , course_id ) ;
304+ if ( ! permissions . can_create_tags ) {
305+ return c . json ( { error : "Unauthorized" } , 401 ) ;
306+ }
307+
308+ const tag = await createTag ( completeNewTagData , course_id ) ;
309+ return c . json ( { tag : tag } , 200 ) ;
310+ } catch ( error ) {
311+ console . error ( error ) ;
312+ if ( error instanceof NotFoundError ) {
313+ return c . json ( { error : error . message } , 404 ) ;
314+ }
315+ return c . json ( { error : "Internal server error" } , 500 ) ;
316+ }
317+ } ) ;
318+
319+ // Update tag
320+ // Note that if user wants to update permission or can_use_tag, they must pass the whole permission/can_use_tag object
321+ app . put ( "/:course_id/tags/:tag_id" , async ( c : Context < { Variables : UserVariables } > ) => {
322+ try {
323+ const course_id = c . req . param ( "course_id" ) ;
324+ const tag_id = c . req . param ( "tag_id" ) ;
325+ const user = c . var . user ;
326+ const updateTagData = await c . req . json ( ) ;
327+
328+ const validationResult = updateTagSchema . safeParse ( updateTagData ) ;
329+ if ( ! validationResult . success ) {
330+ return c . json ( {
331+ error : "Validation failed" ,
332+ details : validationResult . error . errors ,
333+ } , 400 ) ;
334+ }
335+
336+ const completeUpdateTagData = validationResult . data ;
337+
338+ const permissions = await getUserPermissionsInCourse ( user . id , course_id ) ;
339+ if ( ! permissions . can_edit_tags ) {
340+ return c . json ( { error : "Unauthorized" } , 401 ) ;
341+ }
342+
343+ await updateTag ( completeUpdateTagData , tag_id ) ;
344+ return c . json ( 200 ) ;
345+ } catch ( error ) {
346+ console . error ( error ) ;
347+ if ( error instanceof NotFoundError ) {
348+ return c . json ( { error : error . message } , 404 ) ;
349+ }
350+ return c . json ( { error : "Internal server error" } , 500 ) ;
351+ }
352+ } ) ;
353+
354+ // Delete tag
355+ app . delete ( "/:course_id/tags/:tag_id" , async ( c : Context < { Variables : UserVariables } > ) => {
356+ try {
357+ const course_id = c . req . param ( "course_id" ) ;
358+ const tag_id = c . req . param ( "tag_id" ) ;
359+ const user = c . var . user ;
360+
361+ const permissions = await getUserPermissionsInCourse ( user . id , course_id ) ;
362+ if ( ! permissions . can_delete_tags ) {
363+ return c . json ( { error : "Unauthorized" } , 401 ) ;
364+ }
365+
366+ await deleteTag ( tag_id , course_id ) ;
367+ return c . json ( 200 ) ;
368+ } catch ( error ) {
369+ console . error ( error ) ;
370+ return c . json ( { error : "Internal server error" } , 500 ) ;
371+ }
372+ } ) ;
373+
236374export { app } ;
237375Deno . serve ( app . fetch ) ;
0 commit comments