Skip to content

Commit

Permalink
refactor: remove UserId from WithoutAutofieldEntry (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored Aug 13, 2024
1 parent 9177832 commit 0883651
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 175 deletions.
1 change: 0 additions & 1 deletion internal/record/record_chapter_without_autofield_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ type ChapterWithoutAutofieldEntry struct {
Name string
Number int
Sections []SectionWithoutAutofieldEntry
UserId string
}
1 change: 0 additions & 1 deletion internal/record/record_paper_without_autofield_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ package record

type PaperWithoutAutofieldEntry struct {
Content string
UserId string
}
1 change: 0 additions & 1 deletion internal/record/record_project_without_autofield_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package record
type ProjectWithoutAutofieldEntry struct {
Name string
Description string
UserId string
}
5 changes: 2 additions & 3 deletions internal/record/record_section_without_autofield_entry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package record

type SectionWithoutAutofieldEntry struct {
Id string
Name string
UserId string
Id string
Name string
}
12 changes: 8 additions & 4 deletions internal/repository/chapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ type ChapterRepository interface {
chapterId string,
) (*record.ChapterEntry, *Error)
InsertChapter(
userId string,
projectId string,
entry record.ChapterWithoutAutofieldEntry,
) (string, *record.ChapterEntry, *Error)
UpdateChapter(
userId string,
projectId string,
chapterId string,
entry record.ChapterWithoutAutofieldEntry,
Expand Down Expand Up @@ -133,10 +135,11 @@ func (r chapterRepository) FetchChapter(
}

func (r chapterRepository) InsertChapter(
userId string,
projectId string,
entry record.ChapterWithoutAutofieldEntry,
) (string, *record.ChapterEntry, *Error) {
projectValues, rErr := r.projectValues(entry.UserId, projectId)
projectValues, rErr := r.projectValues(userId, projectId)
if rErr != nil {
return "", nil, rErr
}
Expand Down Expand Up @@ -191,15 +194,16 @@ func (r chapterRepository) InsertChapter(
return "", nil, Errorf(ReadFailurePanic, "failed to convert snapshot to values: %w", err)
}

return ref.ID, r.valuesToEntry(values, entry.Number, entry.UserId), nil
return ref.ID, r.valuesToEntry(values, entry.Number, userId), nil
}

func (r chapterRepository) UpdateChapter(
userId string,
projectId string,
chapterId string,
entry record.ChapterWithoutAutofieldEntry,
) (*record.ChapterEntry, *Error) {
projectValues, rErr := r.projectValues(entry.UserId, projectId)
projectValues, rErr := r.projectValues(userId, projectId)
if rErr != nil {
return nil, rErr
}
Expand Down Expand Up @@ -270,7 +274,7 @@ func (r chapterRepository) UpdateChapter(
return nil, Errorf(ReadFailurePanic, "failed to convert snapshot to values: %w", err)
}

return r.valuesToEntry(values, entry.Number, entry.UserId), nil
return r.valuesToEntry(values, entry.Number, userId), nil
}

func (r chapterRepository) projectValues(userId string, projectId string) (*document.ProjectValues, *Error) {
Expand Down
62 changes: 27 additions & 35 deletions internal/repository/chapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,16 @@ func TestInsertChapterValidEntry(t *testing.T) {
client := db.FirestoreClient()
r := repository.NewChapterRepository(*client)

userId := testutil.ModifyOnlyUserId()
projectId := "PROJECT_WITH_DESCRIPTION_TO_UPDATE_FROM_REPOSITORY"

entry := record.ChapterWithoutAutofieldEntry{
Name: "Chapter One",
Number: 1,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: testutil.ModifyOnlyUserId(),
}

id1, createdChapter1, rErr := r.InsertChapter(projectId, entry)
id1, createdChapter1, rErr := r.InsertChapter(userId, projectId, entry)
now := time.Now()

assert.Nil(t, rErr)
Expand All @@ -313,10 +313,9 @@ func TestInsertChapterValidEntry(t *testing.T) {
Name: "Chapter Three",
Number: 2,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: testutil.ModifyOnlyUserId(),
}

id3, createdChapter3, rErr := r.InsertChapter(projectId, entry)
id3, createdChapter3, rErr := r.InsertChapter(userId, projectId, entry)
now = time.Now()

assert.Nil(t, rErr)
Expand All @@ -333,10 +332,9 @@ func TestInsertChapterValidEntry(t *testing.T) {
Name: "Chapter Two",
Number: 2,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: testutil.ModifyOnlyUserId(),
}

id2, createdChapter2, rErr := r.InsertChapter(projectId, entry)
id2, createdChapter2, rErr := r.InsertChapter(userId, projectId, entry)
now = time.Now()

assert.Nil(t, rErr)
Expand All @@ -353,10 +351,9 @@ func TestInsertChapterValidEntry(t *testing.T) {
Name: "Chapter Zero",
Number: 1,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: testutil.ModifyOnlyUserId(),
}

id0, createdChapter0, rErr := r.InsertChapter(projectId, entry)
id0, createdChapter0, rErr := r.InsertChapter(userId, projectId, entry)

assert.Nil(t, rErr)

Expand Down Expand Up @@ -434,11 +431,10 @@ func TestInsertChapterProjectNotFound(t *testing.T) {
client := db.FirestoreClient()
r := repository.NewChapterRepository(*client)

id, createdChapter, rErr := r.InsertChapter(tc.projectId, record.ChapterWithoutAutofieldEntry{
id, createdChapter, rErr := r.InsertChapter(tc.userId, tc.projectId, record.ChapterWithoutAutofieldEntry{
Name: "Chapter One",
Number: 1,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: tc.userId,
})

assert.NotNil(t, rErr)
Expand All @@ -454,18 +450,19 @@ func TestInsertChapterProjectNotFound(t *testing.T) {
func TestInsertChapterInvalidArgument(t *testing.T) {
tt := []struct {
name string
userId string
projectId string
entry record.ChapterWithoutAutofieldEntry
expectedError string
}{
{
name: "should return error when chapter number is too large",
userId: testutil.ModifyOnlyUserId(),
projectId: "PROJECT_WITH_DESCRIPTION_TO_UPDATE_FROM_REPOSITORY",
entry: record.ChapterWithoutAutofieldEntry{
Name: "Chapter Ninety-Nine",
Number: 99,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: testutil.ModifyOnlyUserId(),
},
expectedError: "chapter number is too large",
},
Expand All @@ -476,7 +473,7 @@ func TestInsertChapterInvalidArgument(t *testing.T) {
client := db.FirestoreClient()
r := repository.NewChapterRepository(*client)

id, createdChapter, rErr := r.InsertChapter(tc.projectId, tc.entry)
id, createdChapter, rErr := r.InsertChapter(tc.userId, tc.projectId, tc.entry)

assert.NotNil(t, rErr)

Expand All @@ -492,32 +489,29 @@ func TestUpdateChapterValidEntry(t *testing.T) {
client := db.FirestoreClient()
r := repository.NewChapterRepository(*client)

userId := testutil.ModifyOnlyUserId()
projectId := "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_REPOSITORY"

entry := record.ChapterWithoutAutofieldEntry{
Name: "Chapter One",
Number: 1,
Sections: []record.SectionWithoutAutofieldEntry{
{
Id: "SECTION_ONE",
Name: "Section One",
UserId: testutil.ModifyOnlyUserId(),
Id: "SECTION_ONE",
Name: "Section One",
},
{
Id: "SECTION_TWO",
Name: "Section Two",
UserId: testutil.ModifyOnlyUserId(),
Id: "SECTION_TWO",
Name: "Section Two",
},
{
Id: "SECTION_THREE",
Name: "Section Three",
UserId: testutil.ModifyOnlyUserId(),
Id: "SECTION_THREE",
Name: "Section Three",
},
},
UserId: testutil.ModifyOnlyUserId(),
}

updatedChapter2, rErr := r.UpdateChapter(projectId, "CHAPTER_TWO", entry)
updatedChapter2, rErr := r.UpdateChapter(userId, projectId, "CHAPTER_TWO", entry)
now := time.Now()

assert.Nil(t, rErr)
Expand Down Expand Up @@ -599,10 +593,9 @@ func TestUpdateChapterValidEntry(t *testing.T) {
Name: "Chapter Two",
Number: 2,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: testutil.ModifyOnlyUserId(),
}

updatedChapter1, rErr := r.UpdateChapter(projectId, "CHAPTER_ONE", entry)
updatedChapter1, rErr := r.UpdateChapter(userId, projectId, "CHAPTER_ONE", entry)

assert.Nil(t, rErr)

Expand Down Expand Up @@ -696,24 +689,22 @@ func TestUpdateChapterNotFound(t *testing.T) {
r := repository.NewChapterRepository(*client)

updatedChapter, rErr := r.UpdateChapter(
tc.userId,
tc.projectId,
tc.chapterId,
record.ChapterWithoutAutofieldEntry{
Name: "Chapter One",
Number: 1,
Sections: []record.SectionWithoutAutofieldEntry{
{
Id: "SECTION_ONE",
Name: "Section One",
UserId: testutil.ModifyOnlyUserId(),
Id: "SECTION_ONE",
Name: "Section One",
},
{
Id: "SECTION_TWO",
Name: "Section Two",
UserId: testutil.ModifyOnlyUserId(),
Id: "SECTION_TWO",
Name: "Section Two",
},
},
UserId: tc.userId,
})

assert.NotNil(t, rErr)
Expand All @@ -728,20 +719,21 @@ func TestUpdateChapterNotFound(t *testing.T) {
func TestUpdateChapterInvalidArgument(t *testing.T) {
tt := []struct {
name string
userId string
projectId string
chapterId string
entry record.ChapterWithoutAutofieldEntry
expectedError string
}{
{
name: "should return error when chapter number is too large",
userId: testutil.ModifyOnlyUserId(),
projectId: "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_REPOSITORY",
chapterId: "CHAPTER_ONE",
entry: record.ChapterWithoutAutofieldEntry{
Name: "Chapter Ninety-Nine",
Number: 99,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: testutil.ModifyOnlyUserId(),
},
expectedError: "chapter number is too large",
},
Expand All @@ -752,7 +744,7 @@ func TestUpdateChapterInvalidArgument(t *testing.T) {
client := db.FirestoreClient()
r := repository.NewChapterRepository(*client)

updatedChapter, rErr := r.UpdateChapter(tc.projectId, tc.chapterId, tc.entry)
updatedChapter, rErr := r.UpdateChapter(tc.userId, tc.projectId, tc.chapterId, tc.entry)

assert.NotNil(t, rErr)

Expand Down Expand Up @@ -787,13 +779,13 @@ func TestUpdateChapterInvalidDocument(t *testing.T) {
r := repository.NewChapterRepository(*client)

updatedChapter, rErr := r.UpdateChapter(
tc.userId,
tc.projectId,
tc.chapterId,
record.ChapterWithoutAutofieldEntry{
Name: "Updated Chapter",
Number: 1,
Sections: []record.SectionWithoutAutofieldEntry{},
UserId: tc.userId,
})

assert.NotNil(t, rErr)
Expand Down
12 changes: 8 additions & 4 deletions internal/repository/paper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ type PaperRepository interface {
chapterId string,
) (*record.PaperEntry, *Error)
InsertPaper(
userId string,
projectId string,
chapterId string,
entry record.PaperWithoutAutofieldEntry,
) (string, *record.PaperEntry, *Error)
UpdatePaper(
userId string,
projectId string,
chapterId string,
entry record.PaperWithoutAutofieldEntry,
Expand Down Expand Up @@ -69,11 +71,12 @@ func (r paperRepository) FetchPaper(
}

func (r paperRepository) InsertPaper(
userId string,
projectId string,
chapterId string,
entry record.PaperWithoutAutofieldEntry,
) (string, *record.PaperEntry, *Error) {
_, rErr := r.chapterRepository.FetchChapter(entry.UserId, projectId, chapterId)
_, rErr := r.chapterRepository.FetchChapter(userId, projectId, chapterId)
if rErr != nil {
return "", nil, rErr
}
Expand Down Expand Up @@ -104,15 +107,16 @@ func (r paperRepository) InsertPaper(
return "", nil, Errorf(ReadFailurePanic, "failed to convert snapshot to values: %w", err)
}

return chapterId, r.valuesToEntry(values, entry.UserId), nil
return chapterId, r.valuesToEntry(values, userId), nil
}

func (r paperRepository) UpdatePaper(
userId string,
projectId string,
chapterId string,
entry record.PaperWithoutAutofieldEntry,
) (*record.PaperEntry, *Error) {
_, rErr := r.chapterRepository.FetchChapter(entry.UserId, projectId, chapterId)
_, rErr := r.chapterRepository.FetchChapter(userId, projectId, chapterId)
if rErr != nil {
return nil, rErr
}
Expand Down Expand Up @@ -142,7 +146,7 @@ func (r paperRepository) UpdatePaper(
return nil, Errorf(ReadFailurePanic, "failed to convert snapshot to values: %w", err)
}

return r.valuesToEntry(values, entry.UserId), nil
return r.valuesToEntry(values, userId), nil
}

func (r paperRepository) valuesToEntry(
Expand Down
Loading

0 comments on commit 0883651

Please sign in to comment.