Skip to content

Commit e1d3baf

Browse files
committed
fixed
1 parent f06efdb commit e1d3baf

File tree

6 files changed

+23
-21
lines changed

6 files changed

+23
-21
lines changed

initials/initial_data.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ func createBook(db *gorm.DB, author models.User, genre models.Genre, tag models.
178178
db.Omit("Tags.*").FirstOrCreate(&book, bookToCreate)
179179
books := []models.Book{}
180180
db.Find(&books)
181-
for _, book := range books {
182-
subSection := subSections[rand.Intn(len(subSections))]
183-
book.SubSectionID = subSection.ID
184-
db.Save(&book)
185-
}
186181
return book
187182
}
188183

managers/books.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ 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+
query = query.Where(models.Book{SubSectionID: &subSection.ID})
5757
}
5858
if tagSlug != "" {
5959
tag := models.Tag{Slug: tagSlug}

models/book.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ type Book struct {
7373
GenreID uuid.UUID
7474
Genre Genre `gorm:"foreignKey:GenreID;constraint:OnDelete:SET NULL;<-:false"`
7575

76-
SubSectionID uuid.UUID
77-
SubSection SubSection `gorm:"foreignKey:SubSectionID;constraint:OnDelete:SET NULL;<-:false"`
76+
SubSectionID *uuid.UUID `gorm:"null"`
77+
SubSection *SubSection `gorm:"foreignKey:SubSectionID;constraint:OnDelete:SET NULL;<-:false"`
7878
OrderInSection uint
7979

8080
Tags []Tag `gorm:"many2many:book_tags"`

routes/admin_books.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ func (ep Endpoint) AddBookToSubSection(c *fiber.Ctx) error {
372372
if err != nil {
373373
return c.Status(404).JSON(err)
374374
}
375-
if book.SubSectionID != subsection.ID {
376-
book.SubSectionID = subsection.ID
375+
if book.SubSectionID != &subsection.ID {
376+
book.SubSectionID = &subsection.ID
377377
book.OrderInSection = uint(len(subsection.Books) + 1)
378378
db.Save(&book)
379379
}

routes/routers.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ func SetupRoutes(app *fiber.App, db *gorm.DB) {
5252
authRouter.Get("/logout/all", endpoint.AuthMiddleware, endpoint.LogoutAll)
5353

5454
// Profile Routes (6)
55-
profilesRouter := api.Group("/profiles")
56-
profilesRouter.Get("/profile/:username", endpoint.AuthMiddleware, endpoint.GetProfile)
57-
profilesRouter.Patch("/update", endpoint.AuthMiddleware, endpoint.UpdateProfile)
58-
profilesRouter.Put("/update-password", endpoint.AuthMiddleware, endpoint.UpdatePassword)
59-
profilesRouter.Get("/profile/:username/follow", endpoint.AuthMiddleware, endpoint.FollowUser)
60-
profilesRouter.Get("/notifications", endpoint.AuthMiddleware, endpoint.GetNotifications)
61-
profilesRouter.Post("/notifications/read", endpoint.AuthMiddleware, endpoint.ReadNotification)
55+
profilesRouter := api.Group("/profiles", endpoint.AuthMiddleware)
56+
profilesRouter.Get("/profile/:username", endpoint.GetProfile)
57+
profilesRouter.Patch("/update", endpoint.UpdateProfile)
58+
profilesRouter.Put("/update-password", endpoint.UpdatePassword)
59+
profilesRouter.Get("/profile/:username/follow", endpoint.FollowUser)
60+
profilesRouter.Get("/notifications", endpoint.GetNotifications)
61+
profilesRouter.Post("/notifications/read", endpoint.ReadNotification)
6262

6363
// Book Routes (24)
6464
bookRouter := api.Group("/books")

schemas/book.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ type BookSchema struct {
110110
Blurb string `json:"blurb"`
111111
AgeDiscretion choices.AgeType `json:"age_discretion"`
112112
Genre GenreWithoutTagSchema `json:"genre"`
113-
Section SectionSchema `json:"section"`
114-
SubSection SubSectionSchema `json:"sub_section"`
113+
Section *SectionSchema `json:"section"`
114+
SubSection *SubSectionSchema `json:"sub_section"`
115115
Tags []TagSchema `json:"tags"`
116116
ChaptersCount int `json:"chapters_count"`
117117
PartialViewChapter *ChapterListSchema `json:"partial_view_chapter"`
@@ -143,8 +143,15 @@ func (b BookSchema) Init(book models.Book) BookSchema {
143143
b.Title = book.Title
144144
b.Slug = book.Slug
145145
b.Genre = b.Genre.Init(book.Genre)
146-
b.Section = b.Section.Init(book.SubSection.Section)
147-
b.SubSection = b.SubSection.Init(book.SubSection)
146+
if book.SubSection != nil {
147+
section := b.Section.Init(book.SubSection.Section)
148+
subsection := b.SubSection.Init(*book.SubSection)
149+
b.Section = &section
150+
b.SubSection = &subsection
151+
} else {
152+
b.Section = nil
153+
b.SubSection = nil
154+
}
148155
b.ChaptersCount = book.ChaptersCount()
149156
b.Votes = book.VotesCount()
150157
b.Reads = book.ReadsCount()

0 commit comments

Comments
 (0)