Skip to content

Commit

Permalink
fix(courses): Remove Update By Code
Browse files Browse the repository at this point in the history
  • Loading branch information
marianysilva committed Mar 6, 2024
1 parent 2dacd3b commit 5109117
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 62 deletions.
14 changes: 4 additions & 10 deletions internal/course/database/course_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (

const (
returning_columns = "uuid, code, name, underline, image, image_cover, excerpt, description, created_at, updated_at"
// CREATE
// CREATE.
createCourse = "create course"
// READ
// READ.
listCourse = "list course"
getCourse = "get course by uuid"
// UPDATE
// UPDATE.
updateCourseByUUID = "update course by uuid"
updateCourseByCode = "update course by code"
// DELETE
// DELETE.
deleteCourse = "delete course by uuid"
)

Expand All @@ -34,11 +33,6 @@ func queriesCourse() map[string]string {
WHERE uuid = $8
AND deleted_at IS NULL
RETURNING %s`, returning_columns),
updateCourseByCode: fmt.Sprintf(`UPDATE courses SET
name = $1, underline = $2, image = $3, image_cover = $4, excerpt = $5, description = $6
WHERE code = $7
AND deleted_at IS NULL
RETURNING %s`, returning_columns),
// DELETE
deleteCourse: "UPDATE courses SET deleted_at = NOW() WHERE uuid = $1 AND deleted_at IS NULL",
}
Expand Down
24 changes: 0 additions & 24 deletions internal/course/database/course_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,6 @@ func (r CourseRepository) UpdateCourseByUUID(c *domain.Course) error {
return nil
}

func (r CourseRepository) UpdateCourseByCode(c *domain.Course) error {
stmt, err := r.statement(updateCourseByCode)
if err != nil {
return err
}

args := []interface{}{
// set
c.Name,
c.Underline,
c.Image,
c.ImageCover,
c.Excerpt,
c.Description,
// where
c.Code,
}

if err := stmt.Get(c, args...); err != nil {
return errors.WrapErrorf(err, errors.ErrCodeUnknown, "error updating course")
}
return nil
}

// DeleteCourse soft delete the course by given uuid.
func (r CourseRepository) DeleteCourse(courseUUID uuid.UUID) error {
stmt, err := r.statement(deleteCourse)
Expand Down
2 changes: 0 additions & 2 deletions internal/course/database/course_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,3 @@ func TestRepository_UpdateCourseByUUID(t *testing.T) {
})
}
}

// TODO Test_UpdateCourseByCode
23 changes: 10 additions & 13 deletions internal/course/domain/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ import (
"github.com/google/uuid"
)

// Course struct.
type Course struct {
ID uint `json:"id"`
UUID uuid.UUID `json:"uuid"`
Code string `json:"code"`
Name string `json:"name"`
Underline string `json:"underline"`
Image string `json:"image"`
ImageCover string `db:"image_cover" json:"image_cover"`
Excerpt string `json:"excerpt"`
Description string `json:"description"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
DeletedAt *time.Time `db:"deleted_at" json:"deleted_at"`
UUID uuid.UUID `json:"uuid"`
Code string `json:"code"`
Name string `json:"name"`
Underline string `json:"underline"`
Image string `json:"image"`
ImageCover string `db:"image_cover" json:"image_cover"`
Excerpt string `json:"excerpt"`
Description string `json:"description"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
}
1 change: 0 additions & 1 deletion internal/course/domain/course_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ type CourseRepository interface {
Courses() ([]Course, error)
CreateCourse(c *Course) error
UpdateCourseByUUID(c *Course) error
UpdateCourseByCode(c *Course) error
DeleteCourse(courseUUID uuid.UUID) error
}
13 changes: 1 addition & 12 deletions internal/course/domain/course_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ func (s *Service) CreateCourse(_ context.Context, c *Course) error {
}

func (s *Service) UpdateCourse(_ context.Context, c *Course) error {
exists, err := s.courses.Course(c.UUID)
if err != nil {
return err
}

if exists.Code == c.Code {
err = s.courses.UpdateCourseByCode(c)
} else {
err = s.courses.UpdateCourseByUUID(c)
}

if err != nil {
if err := s.courses.UpdateCourseByUUID(c); err != nil {
return fmt.Errorf("service can't update course: %w", err)
}
return nil
Expand Down

0 comments on commit 5109117

Please sign in to comment.