Skip to content

Commit a22bf70

Browse files
committed
chore: resolve remaining issues
1 parent 56435d7 commit a22bf70

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ linters:
1414
- wrapcheck
1515
# we prefer inline error handling
1616
- noinlineerr
17+
# deprecated, replaced by wsl_v5
18+
- wsl
19+
settings:
20+
wsl_v5:
21+
allow-first-in-block: true
22+
allow-whole-block: false
23+
branch-max-lines: 2

internal/infra/http/handler/student.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package handler
22

33
import (
4+
"crypto/rand"
45
"errors"
5-
"math/rand"
6+
"math/big"
67
"net/http"
78
"strconv"
89

@@ -69,9 +70,6 @@ func (s *Student) Get(c echo.Context) error {
6970
LastName: lnPtr,
7071
EntranceYear: nil,
7172
})
72-
if len(students) == 0 {
73-
return echo.ErrNotFound
74-
}
7573

7674
return c.JSON(http.StatusOK, students)
7775
}
@@ -84,11 +82,16 @@ func (s *Student) Create(c echo.Context) error {
8482
}
8583

8684
if err := req.Validate(); err != nil {
87-
return echo.ErrBadRequest
85+
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
86+
}
87+
88+
// nolint: mnd
89+
randID, err := rand.Int(rand.Reader, big.NewInt(1_000_000))
90+
if err != nil {
91+
return echo.ErrInternalServerError
8892
}
8993

90-
// nolint: gosec, mnd
91-
id := rand.Uint64() % 1_000_000
94+
id := randID.Uint64()
9295

9396
if err := s.repo.Add(c.Request().Context(), model.Student{
9497
ID: id,
@@ -110,5 +113,5 @@ func (s *Student) Create(c echo.Context) error {
110113
func (s *Student) Register(g *echo.Group) {
111114
g.GET("", s.Get)
112115
g.POST("", s.Create)
113-
g.GET(":id", s.GetByID)
116+
g.GET("/:id", s.GetByID)
114117
}

internal/infra/repository/studentsql/student.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
)
1212

1313
type StudentDTO struct {
14-
model.Student
14+
model.Student `gorm:"embedded"`
1515

16+
ID uint64 `gorm:"primaryKey;autoIncrement:false"`
1617
CreatedAt time.Time
1718
UpdatedAt time.Time
1819
DeletedAt gorm.DeletedAt `gorm:"index"`
@@ -30,7 +31,7 @@ func New(db *gorm.DB) *Repository {
3031

3132
func (r *Repository) Add(ctx context.Context, model model.Student) error {
3233
// nolint: exhaustruct
33-
if err := r.db.WithContext(ctx).Create(StudentDTO{Student: model}).Error; err != nil {
34+
if err := r.db.WithContext(ctx).Create(StudentDTO{ID: model.ID, Student: model}).Error; err != nil {
3435
if errors.Is(err, gorm.ErrDuplicatedKey) {
3536
return studentrepo.ErrStudentIDDuplicate
3637
}
@@ -41,7 +42,7 @@ func (r *Repository) Add(ctx context.Context, model model.Student) error {
4142
return nil
4243
}
4344

44-
func (r *Repository) Get(_ context.Context, cmd studentrepo.GetCommand) []model.Student {
45+
func (r *Repository) Get(ctx context.Context, cmd studentrepo.GetCommand) []model.Student {
4546
var studentDTOs []StudentDTO
4647

4748
var condition StudentDTO
@@ -62,14 +63,15 @@ func (r *Repository) Get(_ context.Context, cmd studentrepo.GetCommand) []model.
6263
condition.EntranceYear = *cmd.EntranceYear
6364
}
6465

65-
if err := r.db.Where(&condition).Find(&studentDTOs).Error; err != nil {
66+
if err := r.db.WithContext(ctx).Where(&condition).Find(&studentDTOs).Error; err != nil {
6667
return nil
6768
}
6869

6970
students := make([]model.Student, len(studentDTOs))
7071

7172
for index, dto := range studentDTOs {
7273
students[index] = dto.Student
74+
students[index].ID = dto.ID
7375
}
7476

7577
return students

0 commit comments

Comments
 (0)