Skip to content

Commit

Permalink
fix(courses): apply code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
marianysilva committed Mar 7, 2024
1 parent 54362b2 commit 6244cbf
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions internal/course/database/course_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
)

const (
returning_columns = "uuid, code, name, underline, image, image_cover, excerpt, description, created_at, updated_at"
returningColumns = "uuid, code, name, underline, image, image_cover, excerpt, description, created_at, updated_at"
// CREATE.
createCourse = "create course"
// READ.
listCourse = "list course"
getCourse = "get course by uuid"
// UPDATE.
updateCourseByUUID = "update course by uuid"
updateCourse = "update course by uuid"
// DELETE.
deleteCourse = "delete course by uuid"
)
Expand All @@ -23,16 +23,16 @@ func queriesCourse() map[string]string {
createCourse: fmt.Sprintf(`INSERT INTO courses (
code, name, underline, image, image_cover, excerpt, description
) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING %s`, returning_columns),
RETURNING %s`, returningColumns),
// READ.
listCourse: fmt.Sprintf("SELECT %s FROM courses WHERE deleted_at IS NULL", returning_columns),
getCourse: fmt.Sprintf("SELECT %s FROM courses WHERE uuid = $1 AND deleted_at IS NULL", returning_columns),
listCourse: fmt.Sprintf("SELECT %s FROM courses WHERE deleted_at IS NULL", returningColumns),
getCourse: fmt.Sprintf("SELECT %s FROM courses WHERE uuid = $1 AND deleted_at IS NULL", returningColumns),
// UPDATE.
updateCourseByUUID: fmt.Sprintf(`UPDATE courses SET
updateCourse: fmt.Sprintf(`UPDATE courses SET
code = $1, name = $2, underline = $3, image = $4, image_cover = $5, excerpt = $6, description = $7
WHERE uuid = $8
AND deleted_at IS NULL
RETURNING %s`, returning_columns),
RETURNING %s`, returningColumns),
// DELETE.
deleteCourse: "UPDATE courses SET deleted_at = NOW() WHERE uuid = $1 AND deleted_at IS NULL",
}
Expand Down
6 changes: 3 additions & 3 deletions internal/course/database/course_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func (r CourseRepository) CreateCourse(c *domain.Course) error {
return nil
}

// UpdateCourseByUUID update the given course by ID.
func (r CourseRepository) UpdateCourseByUUID(c *domain.Course) error {
stmt, err := r.statement(updateCourseByUUID)
// UpdateCourse update the given course by ID.
func (r CourseRepository) UpdateCourse(c *domain.Course) error {
stmt, err := r.statement(updateCourse)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/course/database/course_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestRepository_CreateCourse(t *testing.T) {
}
}

func TestRepository_UpdateCourseByUUID(t *testing.T) {
func TestRepository_UpdateCourse(t *testing.T) {
validRows := sqlmock.NewRows([]string{"uuid", "code", "name", "underline", "image", "image_cover", "excerpt",
"description", "created_at", "updated_at"}).
AddRow(course.UUID, course.Code, course.Name, course.Underline, course.Image, course.ImageCover,
Expand Down Expand Up @@ -245,14 +245,14 @@ func TestRepository_UpdateCourseByUUID(t *testing.T) {
if err != nil {
t.Fatalf("an error '%s' was not expected when creating the CourseRepository", err)
}
prep, ok := stmts[updateCourseByUUID]
prep, ok := stmts[updateCourse]
if !ok {
t.Fatalf("prepared statement %s not found", updateCourseByUUID)
t.Fatalf("prepared statement %s not found", updateCourse)
}

prep.ExpectQuery().WillReturnRows(tt.rows)

if err := r.UpdateCourseByUUID(tt.args.c); (err != nil) != tt.wantErr {
if err := r.UpdateCourse(tt.args.c); (err != nil) != tt.wantErr {
t.Errorf("UpdateCourse() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
6 changes: 3 additions & 3 deletions internal/course/domain/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type Course struct {
Code string `json:"code"`
Name string `json:"name"`
Underline string `json:"underline"`
Image string `json:"image"`
ImageCover string `db:"image_cover" json:"image_cover"`
Image string `json:"image,omitempty"`
ImageCover string `db:"image_cover" json:"image_cover,omitempty"`

Check failure on line 15 in internal/course/domain/course.go

View workflow job for this annotation

GitHub Actions / build

tag is not aligned, should be: db:"image_cover" json:"image_cover,omitempty" (tagalign)

Check failure on line 15 in internal/course/domain/course.go

View workflow job for this annotation

GitHub Actions / build

tag is not aligned, should be: db:"image_cover" json:"image_cover,omitempty" (tagalign)
Excerpt string `json:"excerpt"`
Description string `json:"description"`
Description string `json:"description,omitempty"`
CreatedAt time.Time `db:"created_at" json:"created_at"`

Check failure on line 18 in internal/course/domain/course.go

View workflow job for this annotation

GitHub Actions / build

tag is not aligned, should be: db:"created_at" json:"created_at" (tagalign)

Check failure on line 18 in internal/course/domain/course.go

View workflow job for this annotation

GitHub Actions / build

tag is not aligned, should be: db:"created_at" json:"created_at" (tagalign)
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`

Check failure on line 19 in internal/course/domain/course.go

View workflow job for this annotation

GitHub Actions / build

tag is not aligned, should be: db:"updated_at" json:"updated_at" (tagalign)

Check failure on line 19 in internal/course/domain/course.go

View workflow job for this annotation

GitHub Actions / build

tag is not aligned, should be: db:"updated_at" json:"updated_at" (tagalign)
}
2 changes: 1 addition & 1 deletion internal/course/domain/course_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ type CourseRepository interface {
Course(courseUUID uuid.UUID) (Course, error)
Courses() ([]Course, error)
CreateCourse(c *Course) error
UpdateCourseByUUID(c *Course) error
UpdateCourse(c *Course) error
DeleteCourse(courseUUID uuid.UUID) error
}
2 changes: 1 addition & 1 deletion internal/course/domain/course_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *Service) CreateCourse(_ context.Context, c *Course) error {
}

func (s *Service) UpdateCourse(_ context.Context, c *Course) error {
if err := s.courses.UpdateCourseByUUID(c); err != nil {
if err := s.courses.UpdateCourse(c); err != nil {
return fmt.Errorf("service can't update course: %w", err)
}
return nil
Expand Down

0 comments on commit 6244cbf

Please sign in to comment.