Skip to content

Commit a82ca53

Browse files
add sync for current book for club
1 parent ad9b77d commit a82ca53

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

internal/services/reading.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package services
22

33
import (
4-
"slices"
4+
"encoding/json"
55
"errors"
66
"fmt"
7+
"slices"
78
"time"
89

910
"github.com/nevzattalhaozcan/forgotten/internal/config"
@@ -250,6 +251,23 @@ func (s *ReadingService) AssignBookToClub(clubID, bookID uint, req *models.Assig
250251
ID: a.ID, ClubID: clubID, Book: *book, Status: string(a.Status),
251252
StartDate: a.StartDate, DueDate: a.DueDate, TargetPage: a.TargetPage, Checkpoint: a.Checkpoint,
252253
}
254+
255+
book, berr := s.bookRepo.GetByID(bookID)
256+
if berr == nil && book != nil {
257+
cb := models.CurrentBook{
258+
Title: book.Title,
259+
Author: book.Author,
260+
CoverURL: book.CoverURL,
261+
BookID: &book.ID,
262+
}
263+
if b, err := json.Marshal(&cb); err == nil {
264+
if club, cerr := s.clubRepo.GetByID(clubID); cerr == nil {
265+
club.CurrentBook = b
266+
_ = s.clubRepo.Update(club) // non-fatal; don't fail the assignment if club update fails
267+
}
268+
}
269+
}
270+
253271
return resp, nil
254272
}
255273

@@ -272,6 +290,21 @@ func (s *ReadingService) UpdateClubCheckpoint(clubID uint, req *models.UpdateClu
272290
ID: a.ID, ClubID: a.ClubID, Book: *book, Status: string(a.Status),
273291
StartDate: a.StartDate, DueDate: a.DueDate, TargetPage: a.TargetPage, Checkpoint: a.Checkpoint,
274292
}
293+
294+
if req.TargetPage != nil {
295+
if club, cerr := s.clubRepo.GetByID(clubID); cerr == nil && len(club.CurrentBook) > 0 {
296+
var cb models.CurrentBook
297+
if uerr := json.Unmarshal(club.CurrentBook, &cb); uerr == nil {
298+
p := *req.TargetPage
299+
cb.Progress = &p
300+
if b, merr := json.Marshal(&cb); merr == nil {
301+
club.CurrentBook = b
302+
_ = s.clubRepo.Update(club)
303+
}
304+
}
305+
}
306+
}
307+
275308
return resp, nil
276309
}
277310

@@ -291,6 +324,12 @@ func (s *ReadingService) CompleteClubAssignment(clubID uint) (*models.ClubAssign
291324
ID: a.ID, ClubID: a.ClubID, Book: *book, Status: string(a.Status),
292325
StartDate: a.StartDate, DueDate: a.DueDate, TargetPage: a.TargetPage, Checkpoint: a.Checkpoint,
293326
}
327+
328+
if club, cerr := s.clubRepo.GetByID(clubID); cerr == nil {
329+
club.CurrentBook = nil
330+
_ = s.clubRepo.Update(club)
331+
}
332+
294333
return resp, nil
295334
}
296335

0 commit comments

Comments
 (0)