Skip to content

Commit 5f7084c

Browse files
committed
fixes
1 parent 02a22fc commit 5f7084c

File tree

9 files changed

+56
-95
lines changed

9 files changed

+56
-95
lines changed

docs/docs.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5211,14 +5211,14 @@ const docTemplate = `{
52115211
"reviews": {
52125212
"$ref": "#/definitions/schemas.ReviewsResponseDataSchema"
52135213
},
5214-
"section": {
5215-
"$ref": "#/definitions/schemas.SectionSchema"
5216-
},
52175214
"slug": {
52185215
"type": "string"
52195216
},
5220-
"sub_section": {
5221-
"$ref": "#/definitions/schemas.SubSectionSchema"
5217+
"sub_sections": {
5218+
"type": "array",
5219+
"items": {
5220+
"$ref": "#/definitions/schemas.SubSectionSchema"
5221+
}
52225222
},
52235223
"tags": {
52245224
"type": "array",
@@ -5372,14 +5372,14 @@ const docTemplate = `{
53725372
"reads": {
53735373
"type": "integer"
53745374
},
5375-
"section": {
5376-
"$ref": "#/definitions/schemas.SectionSchema"
5377-
},
53785375
"slug": {
53795376
"type": "string"
53805377
},
5381-
"sub_section": {
5382-
"$ref": "#/definitions/schemas.SubSectionSchema"
5378+
"sub_sections": {
5379+
"type": "array",
5380+
"items": {
5381+
"$ref": "#/definitions/schemas.SubSectionSchema"
5382+
}
53835383
},
53845384
"tags": {
53855385
"type": "array",
@@ -6871,9 +6871,6 @@ const docTemplate = `{
68716871
"author": {
68726872
"$ref": "#/definitions/schemas.UserDataSchema"
68736873
},
6874-
"order_in_section": {
6875-
"type": "integer"
6876-
},
68776874
"title": {
68786875
"type": "string"
68796876
}

docs/swagger.json

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5204,14 +5204,14 @@
52045204
"reviews": {
52055205
"$ref": "#/definitions/schemas.ReviewsResponseDataSchema"
52065206
},
5207-
"section": {
5208-
"$ref": "#/definitions/schemas.SectionSchema"
5209-
},
52105207
"slug": {
52115208
"type": "string"
52125209
},
5213-
"sub_section": {
5214-
"$ref": "#/definitions/schemas.SubSectionSchema"
5210+
"sub_sections": {
5211+
"type": "array",
5212+
"items": {
5213+
"$ref": "#/definitions/schemas.SubSectionSchema"
5214+
}
52155215
},
52165216
"tags": {
52175217
"type": "array",
@@ -5365,14 +5365,14 @@
53655365
"reads": {
53665366
"type": "integer"
53675367
},
5368-
"section": {
5369-
"$ref": "#/definitions/schemas.SectionSchema"
5370-
},
53715368
"slug": {
53725369
"type": "string"
53735370
},
5374-
"sub_section": {
5375-
"$ref": "#/definitions/schemas.SubSectionSchema"
5371+
"sub_sections": {
5372+
"type": "array",
5373+
"items": {
5374+
"$ref": "#/definitions/schemas.SubSectionSchema"
5375+
}
53765376
},
53775377
"tags": {
53785378
"type": "array",
@@ -6864,9 +6864,6 @@
68646864
"author": {
68656865
"$ref": "#/definitions/schemas.UserDataSchema"
68666866
},
6867-
"order_in_section": {
6868-
"type": "integer"
6869-
},
68706867
"title": {
68716868
"type": "string"
68726869
}

docs/swagger.yaml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,12 @@ definitions:
269269
type: integer
270270
reviews:
271271
$ref: '#/definitions/schemas.ReviewsResponseDataSchema'
272-
section:
273-
$ref: '#/definitions/schemas.SectionSchema'
274272
slug:
275273
type: string
276-
sub_section:
277-
$ref: '#/definitions/schemas.SubSectionSchema'
274+
sub_sections:
275+
items:
276+
$ref: '#/definitions/schemas.SubSectionSchema'
277+
type: array
278278
tags:
279279
items:
280280
$ref: '#/definitions/schemas.TagSchema'
@@ -379,12 +379,12 @@ definitions:
379379
$ref: '#/definitions/schemas.ChapterListSchema'
380380
reads:
381381
type: integer
382-
section:
383-
$ref: '#/definitions/schemas.SectionSchema'
384382
slug:
385383
type: string
386-
sub_section:
387-
$ref: '#/definitions/schemas.SubSectionSchema'
384+
sub_sections:
385+
items:
386+
$ref: '#/definitions/schemas.SubSectionSchema'
387+
type: array
388388
tags:
389389
items:
390390
$ref: '#/definitions/schemas.TagSchema'
@@ -1404,8 +1404,6 @@ definitions:
14041404
properties:
14051405
author:
14061406
$ref: '#/definitions/schemas.UserDataSchema'
1407-
order_in_section:
1408-
type: integer
14091407
title:
14101408
type: string
14111409
type: object

managers/books.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func (b BookManager) GetLatest(db *gorm.DB, genreSlug string, sectionSlug string
4141
errData := utils.NotFoundErr("Invalid book section")
4242
return books, &errData
4343
}
44-
query = query.Joins("JOIN sub_sections ON sub_sections.id = books.sub_section_id").
45-
Joins("JOIN sections ON sections.id = sub_sections.section_id").
46-
Where("sections.id = ?", section.ID)
44+
query = query.Joins("JOIN book_sub_sections ON book_sub_sections.book_id = books.id").
45+
Joins("JOIN sub_sections ON sub_sections.id = book_sub_sections.sub_section_id").
46+
Where("sub_sections.section_id = ?", section.ID)
4747
joinedSubSections = true
4848
}
4949
if subSectionSlug != "" {
@@ -53,7 +53,10 @@ func (b BookManager) GetLatest(db *gorm.DB, genreSlug string, sectionSlug string
5353
errData := utils.NotFoundErr("Invalid book subsection")
5454
return books, &errData
5555
}
56-
query = query.Where(models.Book{SubSectionID: &subSection.ID})
56+
if !joinedSubSections {
57+
query = query.Joins("JOIN book_sub_sections ON book_sub_sections.book_id = books.id")
58+
}
59+
query = query.Where("book_sub_sections.sub_section_id = ?", subSection.ID)
5760
}
5861
if tagSlug != "" {
5962
tag := models.Tag{Slug: tagSlug}
@@ -103,11 +106,13 @@ func (b BookManager) GetLatest(db *gorm.DB, genreSlug string, sectionSlug string
103106
if orderBySubSection {
104107
if !joinedSubSections {
105108
query = query.
106-
Joins("LEFT JOIN sub_sections ON sub_sections.id = books.sub_section_id")
109+
Joins("LEFT JOIN book_sub_sections ON book_sub_sections.book_id = books.id")
107110
}
108111
query = query.
109-
Group("sub_sections.name").
110-
Order("sub_sections.name ASC")
112+
Joins("LEFT JOIN sub_sections ON sub_sections.id = book_sub_sections.sub_section_id").
113+
Select("books.*, COALESCE(AVG(comments.rating), 0) AS avg_rating, MIN(sub_sections.name) AS sort_name").
114+
Group("books.id").
115+
Order("sort_name ASC")
111116
}
112117
if trending {
113118
// Order by most read books

models/book.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type SubSection struct {
5757
BaseModel
5858
Name string `gorm:"unique"`
5959
Slug string `gorm:"unique"`
60-
Books []Book
60+
Books []Book `gorm:"many2many:book_sub_sections"`
6161
SectionID uuid.UUID
6262
Section Section `gorm:"foreignKey:SectionID;constraint:OnDelete:SET NULL;<-:false"`
6363
}
@@ -79,9 +79,7 @@ type Book struct {
7979
GenreID uuid.UUID
8080
Genre Genre `gorm:"foreignKey:GenreID;constraint:OnDelete:SET NULL;<-:false"`
8181

82-
SubSectionID *uuid.UUID `gorm:"null"`
83-
SubSection *SubSection `gorm:"foreignKey:SubSectionID;constraint:OnDelete:SET NULL;<-:false"`
84-
OrderInSection uint
82+
SubSections []SubSection `gorm:"many2many:book_sub_sections"`
8583

8684
Tags []Tag `gorm:"many2many:book_tags"`
8785
Chapters []Chapter `gorm:"constraint:OnDelete:CASCADE"`

models/scopes/scopes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ func FollowerFollowingBooksPreloaderScope(db *gorm.DB) *gorm.DB {
2222
}
2323

2424
func AuthorGenreTagBookScope(db *gorm.DB) *gorm.DB {
25-
return db.Joins("Author").Joins("Genre").Joins("SubSection").Joins("SubSection.Section").Preload("Tags").Preload("Chapters").Preload("Votes").Preload("Reads")
25+
return db.Joins("Author").Joins("Genre").Preload("SubSections").Preload("SubSections.Section").Preload("Tags").Preload("Chapters").Preload("Votes").Preload("Reads")
2626
}
2727

2828
func AuthorGenreTagBookPreloadScope(db *gorm.DB) *gorm.DB {
29-
return db.Preload("Author").Preload("Genre").Preload("SubSection").Preload("SubSection.Section").Preload("Tags").Preload("Chapters").Preload("Votes").Preload("Reads")
29+
return db.Preload("Author").Preload("Genre").Preload("SubSections").Preload("SubSections.Section").Preload("Tags").Preload("Chapters").Preload("Votes").Preload("Reads")
3030
}
3131

3232
func TagsChaptersVotesBookScope(db *gorm.DB) *gorm.DB {

routes/admin_books.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/LitPad/backend/utils"
88
"github.com/gofiber/fiber/v2"
99
"github.com/google/uuid"
10-
"gorm.io/gorm"
1110
)
1211

1312
// @Summary Add Genre
@@ -363,11 +362,7 @@ func (ep Endpoint) AddBookToSubSection(c *fiber.Ctx) error {
363362
if err != nil {
364363
return c.Status(404).JSON(err)
365364
}
366-
if book.SubSectionID != &subsection.ID {
367-
book.SubSectionID = &subsection.ID
368-
book.OrderInSection = uint(len(subsection.Books) + 1)
369-
db.Save(&book)
370-
}
365+
db.Model(&book).Association("SubSections").Append(subsection)
371366
return c.Status(200).JSON(ResponseMessage("Book added to subsection successfully"))
372367
}
373368

@@ -395,31 +390,7 @@ func (ep Endpoint) RemoveBookFromSubSection(c *fiber.Ctx) error {
395390
if err != nil {
396391
return c.Status(404).JSON(err)
397392
}
398-
399-
// Get the current order of the book
400-
currentOrder := book.OrderInSection
401-
402-
// Start a transaction to handle the operations atomically
403-
tx := db.Begin()
404-
405-
// 1. Set the SubSectionID of the book to nil
406-
if err := tx.Model(&book).Update("SubSectionID", nil).Error; err != nil {
407-
tx.Rollback()
408-
return c.Status(500).JSON(utils.ServerErr("Failed to remove book from subsection"))
409-
}
410-
411-
// 2. Rearrange the OrderInSection for books after the removed book
412-
if err := tx.Model(&models.Book{}).
413-
Where("sub_section_id = ? AND order_in_section > ?", subsection.ID, currentOrder).
414-
Update("order_in_section", gorm.Expr("order_in_section - 1")).Error; err != nil {
415-
tx.Rollback()
416-
return c.Status(500).JSON(utils.ServerErr("Failed to update book order"))
417-
}
418-
419-
// Commit the transaction
420-
tx.Commit()
421-
422-
// Return a success message
393+
db.Model(&book).Association("SubSections").Delete(subsection)
423394
return c.Status(200).JSON(ResponseMessage("Book removed from subsection"))
424395
}
425396

schemas/admin_books.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ func (s SectionsWithSubSectionsSchema) Init(sections []models.Section) SectionsW
107107
}
108108

109109
type SubSectionBookSchema struct {
110-
OrderInSection uint `json:"order_in_section"`
111110
Title string `json:"title"`
112111
Author UserDataSchema `json:"author"`
113112
}
@@ -129,7 +128,6 @@ func (s SubSectionWithBooksSchema) Init(subSection models.SubSection, books []mo
129128
bookItems := []SubSectionBookSchema{}
130129
for _, item := range books {
131130
bookItems = append(bookItems, SubSectionBookSchema{
132-
OrderInSection: item.OrderInSection,
133131
Title: item.Title,
134132
Author: UserDataSchema{}.Init(item.Author),
135133
})

schemas/book.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ func (g GenreSchema) Init(genre models.Genre) GenreSchema {
7272
}
7373

7474
type ChapterListSchema struct {
75-
Title string `json:"title"`
76-
Slug string `json:"slug"`
77-
IsLast bool `json:"is_last"`
75+
Title string `json:"title"`
76+
Slug string `json:"slug"`
77+
IsLast bool `json:"is_last"`
7878
}
7979

8080
func (c ChapterListSchema) Init(chapter models.Chapter) ChapterListSchema {
@@ -114,8 +114,7 @@ type BookSchema struct {
114114
Blurb string `json:"blurb"`
115115
AgeDiscretion choices.AgeType `json:"age_discretion"`
116116
Genre GenreWithoutTagSchema `json:"genre"`
117-
Section *SectionSchema `json:"section"`
118-
SubSection *SubSectionSchema `json:"sub_section"`
117+
SubSections []SubSectionSchema `json:"sub_sections"`
119118
Tags []TagSchema `json:"tags"`
120119
ChaptersCount int `json:"chapters_count"`
121120
PartialViewChapter *ChapterListSchema `json:"partial_view_chapter"`
@@ -147,15 +146,13 @@ func (b BookSchema) Init(book models.Book) BookSchema {
147146
b.Title = book.Title
148147
b.Slug = book.Slug
149148
b.Genre = b.Genre.Init(book.Genre)
150-
if book.SubSection != nil {
151-
section := SectionSchema{}.Init(book.SubSection.Section)
152-
subsection := SubSectionSchema{}.Init(book.SubSection)
153-
b.Section = &section
154-
b.SubSection = &subsection
155-
} else {
156-
b.Section = nil
157-
b.SubSection = nil
149+
150+
subsections := book.SubSections
151+
subsectionsToAdd := make([]SubSectionSchema, 0)
152+
for _, subsection := range subsections {
153+
subsectionsToAdd = append(subsectionsToAdd, SubSectionSchema{}.Init(&subsection))
158154
}
155+
b.SubSections = subsectionsToAdd
159156
b.ChaptersCount = book.ChaptersCount()
160157
b.Votes = book.VotesCount()
161158
b.Reads = book.ReadsCount()

0 commit comments

Comments
 (0)