Skip to content

Commit

Permalink
refactor: define ChapterRepository.FetchChapter (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored Aug 12, 2024
1 parent bd51c88 commit 9177832
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 100 deletions.
2 changes: 1 addition & 1 deletion fixtures/firebase-export-metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "13.13.0",
"version": "13.15.1",
"firestore": {
"version": "1.19.7",
"path": "firestore_export",
Expand Down
Binary file not shown.
Binary file modified fixtures/firestore_export/all_namespaces/all_kinds/output-0
Binary file not shown.
Binary file not shown.
57 changes: 51 additions & 6 deletions internal/repository/chapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ import (
const ChapterCollection = "chapters"

type ChapterRepository interface {
FetchProjectChapters(
FetchChapters(
userId string,
projectId string,
) (map[string]record.ChapterEntry, *Error)
FetchChapter(
userId string,
projectId string,
chapterId string,
) (*record.ChapterEntry, *Error)
InsertChapter(
projectId string,
entry record.ChapterWithoutAutofieldEntry,
Expand All @@ -38,7 +43,7 @@ func NewChapterRepository(client firestore.Client) ChapterRepository {
return chapterRepository{client: client}
}

func (r chapterRepository) FetchProjectChapters(
func (r chapterRepository) FetchChapters(
userId string,
projectId string,
) (map[string]record.ChapterEntry, *Error) {
Expand Down Expand Up @@ -87,6 +92,46 @@ func (r chapterRepository) FetchProjectChapters(
return entries, nil
}

func (r chapterRepository) FetchChapter(
userId string,
projectId string,
chapterId string,
) (*record.ChapterEntry, *Error) {
projectValues, rErr := r.projectValues(userId, projectId)
if rErr != nil {
return nil, rErr
}

chapterNumbers := make(map[string]int)
for i, chapterId := range projectValues.ChapterIds {
chapterNumbers[chapterId] = i + 1
}

snapshot, err := r.client.Collection(ProjectCollection).
Doc(projectId).
Collection(ChapterCollection).
Doc(chapterId).
Get(db.FirestoreContext())

if err != nil {
return nil, Errorf(NotFoundError, "failed to fetch chapter")
}

var values document.ChapterValues
err = snapshot.DataTo(&values)
if err != nil {
return nil, Errorf(ReadFailurePanic, "failed to convert snapshot to values: %w", err)
}

number, ok := chapterNumbers[snapshot.Ref.ID]
if !ok {
err = fmt.Errorf("%v.chapterIds have deficient elements", reflect.TypeOf(*projectValues))
return nil, Errorf(ReadFailurePanic, "failed to convert values to entry: %w", err)
}

return r.valuesToEntry(values, number, userId), nil
}

func (r chapterRepository) InsertChapter(
projectId string,
entry record.ChapterWithoutAutofieldEntry,
Expand Down Expand Up @@ -137,7 +182,7 @@ func (r chapterRepository) InsertChapter(

snapshot, err := ref.Get(db.FirestoreContext())
if err != nil {
return "", nil, Errorf(WriteFailurePanic, "failed to get inserted chapter: %w", err)
return "", nil, Errorf(WriteFailurePanic, "failed to fetch inserted chapter: %w", err)
}

var values document.ChapterValues
Expand Down Expand Up @@ -216,7 +261,7 @@ func (r chapterRepository) UpdateChapter(
Doc(chapterId).
Get(db.FirestoreContext())
if err != nil {
return nil, Errorf(ReadFailurePanic, "failed to get updated chapter: %w", err)
return nil, Errorf(ReadFailurePanic, "failed to fetch updated chapter: %w", err)
}

var values document.ChapterValues
Expand All @@ -234,7 +279,7 @@ func (r chapterRepository) projectValues(userId string, projectId string) (*docu

snapshot, err := ref.Get(db.FirestoreContext())
if err != nil {
return nil, Errorf(NotFoundError, "project not found")
return nil, Errorf(NotFoundError, "failed to fetch project")
}

var projectValues document.ProjectValues
Expand All @@ -244,7 +289,7 @@ func (r chapterRepository) projectValues(userId string, projectId string) (*docu
}

if projectValues.UserId != userId {
return nil, Errorf(NotFoundError, "project not found")
return nil, Errorf(NotFoundError, "failed to fetch project")
}

if projectValues.ChapterIds == nil {
Expand Down
Loading

0 comments on commit 9177832

Please sign in to comment.