77 "github.com/LitPad/backend/utils"
88 "github.com/gofiber/fiber/v2"
99 "github.com/google/uuid"
10+ "gorm.io/gorm"
1011)
1112
1213// @Summary Add Genre
@@ -218,7 +219,7 @@ func (ep Endpoint) AdminUpdateBookSection(c *fiber.Ctx) error {
218219// @Security BearerAuth
219220func (ep Endpoint ) AdminUpdateBookSubSection (c * fiber.Ctx ) error {
220221 db := ep .DB
221- subsection := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
222+ subsection , _ := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
222223 if subsection == nil {
223224 return c .Status (404 ).JSON (utils .NotFoundErr ("SubSection does not exist" ))
224225 }
@@ -332,7 +333,7 @@ func (ep Endpoint) AdminDeleteBookSection(c *fiber.Ctx) error {
332333// @Security BearerAuth
333334func (ep Endpoint ) AdminDeleteBookSubSection (c * fiber.Ctx ) error {
334335 db := ep .DB
335- subsection := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
336+ subsection , _ := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
336337 if subsection == nil {
337338 return c .Status (404 ).JSON (utils .NotFoundErr ("SubSection does not exist" ))
338339 }
@@ -354,15 +355,44 @@ func (ep Endpoint) AdminDeleteBookSubSection(c *fiber.Ctx) error {
354355// @Security BearerAuth
355356func (ep Endpoint ) AddBookToSubSection (c * fiber.Ctx ) error {
356357 db := ep .DB
357- subsection := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
358+
359+ subsection , _ := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
358360 if subsection == nil {
359361 return c .Status (404 ).JSON (utils .NotFoundErr ("SubSection does not exist" ))
360362 }
363+
361364 book , err := bookManager .GetBySlug (db , c .Params ("book_slug" ), false )
362365 if err != nil {
363366 return c .Status (404 ).JSON (err )
364367 }
365- db .Model (& book ).Association ("SubSections" ).Append (subsection )
368+
369+ // Check if already exists to prevent duplicates
370+ var count int64
371+ db .Model (& models.BookSubSection {}).
372+ Where ("book_id = ? AND sub_section_id = ?" , book .ID , subsection .ID ).
373+ Count (& count )
374+
375+ if count > 0 {
376+ return c .Status (200 ).JSON (ResponseMessage ("Book already in subsection" ))
377+ }
378+
379+ // Determine order in section
380+ var order int64
381+ db .Model (& models.BookSubSection {}).
382+ Where ("sub_section_id = ?" , subsection .ID ).
383+ Count (& order )
384+
385+ // Add association manually to maintain ordering
386+ bookSub := models.BookSubSection {
387+ BookID : book .ID ,
388+ SubSectionID : subsection .ID ,
389+ OrderInSection : uint (order + 1 ),
390+ }
391+
392+ if err := db .Create (& bookSub ).Error ; err != nil {
393+ return c .Status (500 ).JSON (utils .ServerErr ("Something went wrong" ))
394+ }
395+
366396 return c .Status (200 ).JSON (ResponseMessage ("Book added to subsection successfully" ))
367397}
368398
@@ -380,18 +410,43 @@ func (ep Endpoint) AddBookToSubSection(c *fiber.Ctx) error {
380410// @Security BearerAuth
381411func (ep Endpoint ) RemoveBookFromSubSection (c * fiber.Ctx ) error {
382412 db := ep .DB
383- subsection := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
413+
414+ subsection , _ := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
384415 if subsection == nil {
385416 return c .Status (404 ).JSON (utils .NotFoundErr ("SubSection does not exist" ))
386417 }
387418
388- // Get the book by its slug
389419 book , err := bookManager .GetBySlug (db , c .Params ("book_slug" ), false )
390420 if err != nil {
391421 return c .Status (404 ).JSON (err )
392422 }
393- db .Model (& book ).Association ("SubSections" ).Delete (subsection )
394- return c .Status (200 ).JSON (ResponseMessage ("Book removed from subsection" ))
423+
424+ var bookSub models.BookSubSection
425+ if err := db .
426+ Where ("book_id = ? AND sub_section_id = ?" , book .ID , subsection .ID ).
427+ First (& bookSub ).Error ; err != nil {
428+ return c .Status (404 ).JSON (utils .NotFoundErr ("Book not found in subsection" ))
429+ }
430+
431+ tx := db .Begin ()
432+
433+ // Delete the record
434+ if err := tx .Delete (& bookSub ).Error ; err != nil {
435+ tx .Rollback ()
436+ return c .Status (500 ).JSON (utils .ServerErr ("Failed to remove book from subsection" ))
437+ }
438+
439+ // Shift order up for other books in that subsection
440+ if err := tx .Model (& models.BookSubSection {}).
441+ Where ("sub_section_id = ? AND order_in_section > ?" , subsection .ID , bookSub .OrderInSection ).
442+ Update ("order_in_section" , gorm .Expr ("order_in_section - 1" )).Error ; err != nil {
443+ tx .Rollback ()
444+ return c .Status (500 ).JSON (utils .ServerErr ("Failed to update book order" ))
445+ }
446+
447+ tx .Commit ()
448+
449+ return c .Status (200 ).JSON (ResponseMessage ("Book removed from subsection successfully" ))
395450}
396451
397452// @Summary Delete Tag
@@ -447,20 +502,20 @@ func (ep Endpoint) AdminGetSections(c *fiber.Ctx) error {
447502// @Security BearerAuth
448503func (ep Endpoint ) AdminGetSubSection (c * fiber.Ctx ) error {
449504 db := ep .DB
450- subSection := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
505+ subSection , books := genreManager .GetSubSectionBySlug (db , c .Params ("slug" ))
451506 if subSection == nil {
452507 return c .Status (404 ).JSON (utils .NotFoundErr ("Sub section does not exist" ))
453508 }
454509 // Paginate books
455- paginatedData , paginatedBooks , err := PaginateQueryset (subSection . Books , c , 200 )
510+ paginatedData , paginatedBooks , err := PaginateQueryset (books , c , 200 )
456511 if err != nil {
457512 return c .Status (400 ).JSON (err )
458513 }
459- books := paginatedBooks .([]models. Book )
514+ castedBooks := paginatedBooks .([]schemas. BookWithOrder )
460515
461516 response := schemas.SubSectionWithBooksResponseSchema {
462517 ResponseSchema : ResponseMessage ("Sub section retrieved successfully" ),
463- Data : schemas.SubSectionWithBooksSchema {}.Init (* subSection , books , * paginatedData ),
518+ Data : schemas.SubSectionWithBooksSchema {}.Init (* subSection , castedBooks , * paginatedData ),
464519 }
465520 return c .Status (200 ).JSON (response )
466521}
0 commit comments