Skip to content

Commit

Permalink
z
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed May 26, 2024
1 parent 4fe8017 commit 4665e98
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
12 changes: 12 additions & 0 deletions internal/pkg/gstr/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import (
"github.com/trim21/errgo"
)

func ParseInt8(s string) (int8, error) {
v, err := strconv.ParseInt(s, 10, 8)

return int8(v), errgo.Wrap(err, "strconv")
}

func ParseUint8(s string) (uint8, error) {
v, err := strconv.ParseUint(s, 10, 8)

Expand All @@ -32,6 +38,12 @@ func ParseUint16(s string) (uint16, error) {
return uint16(v), errgo.Wrap(err, "strconv")
}

func ParseInt32(s string) (int32, error) {
v, err := strconv.ParseInt(s, 10, 32)

return int32(v), errgo.Wrap(err, "strconv")
}

func ParseUint32(s string) (uint32, error) {
v, err := strconv.ParseUint(s, 10, 32)

Expand Down
4 changes: 2 additions & 2 deletions internal/subject/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type BrowseFilter struct {
Series null.Bool
Platform null.String
Order null.String
Year null.Uint16
Month null.Uint8
Year null.Int32
Month null.Int8
}

type Repo interface {
Expand Down
74 changes: 70 additions & 4 deletions internal/subject/mysql_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,81 @@ func (r mysqlRepo) GetByIDs(
func (r mysqlRepo) Count(
ctx context.Context,
filter BrowseFilter) (int64, error) {
// TODO:(everpcpc)
return 0, nil
q := r.q.Subject.WithContext(ctx).Joins(r.q.Subject.Fields).Where(r.q.Subject.TypeID.Eq(filter.Type))
if filter.NSFW.Set {
q = q.Where(r.q.Subject.Nsfw.Is(filter.NSFW.Value))
}
if filter.Category.Set {
q = q.Where(r.q.Subject.Platform.Eq(filter.Category.Value))
}
if filter.Series.Set {
q = q.Where(r.q.Subject.Series.Is(filter.Series.Value))
}
// TODO:(everpcpc): add platform filter
if filter.Year.Set {
q = q.Where(r.q.SubjectField.Year.Eq(filter.Year.Value))
}
if filter.Month.Set {
q = q.Where(r.q.SubjectField.Mon.Eq(filter.Month.Value))
}

if filter.Order.Set {
switch filter.Order.Value {
case "date":
q = q.Order(r.q.SubjectField.Date)
case "rank":
q = q.Order(r.q.SubjectField.Rank)
}
}

return q.Count()
}

func (r mysqlRepo) Browse(
ctx context.Context, filter BrowseFilter, limit, offset int,
) ([]model.Subject, error) {
// TODO:(everpcpc)
return []model.Subject{}, nil
q := r.q.Subject.WithContext(ctx).Joins(r.q.Subject.Fields).Where(r.q.Subject.TypeID.Eq(filter.Type))
if filter.NSFW.Set {
q = q.Where(r.q.Subject.Nsfw.Is(filter.NSFW.Value))
}
if filter.Category.Set {
q = q.Where(r.q.Subject.Platform.Eq(filter.Category.Value))
}
if filter.Series.Set {
q = q.Where(r.q.Subject.Series.Is(filter.Series.Value))
}
// TODO:(everpcpc): add platform filter
if filter.Year.Set {
q = q.Where(r.q.SubjectField.Year.Eq(filter.Year.Value))
}
if filter.Month.Set {
q = q.Where(r.q.SubjectField.Mon.Eq(filter.Month.Value))
}

if filter.Order.Set {
switch filter.Order.Value {
case "date":
q = q.Order(r.q.SubjectField.Date)
case "rank":
q = q.Order(r.q.SubjectField.Rank)
}
}

subjects, err := q.Limit(limit).Offset(offset).Find()
if err != nil {
r.log.Error("unexpected error happened", zap.Error(err))
return nil, errgo.Wrap(err, "dal")
}

var result = make([]model.Subject, len(subjects))
for i, subject := range subjects {
result[i], err = ConvertDao(subject)
if err != nil {
return nil, err
}
}

return result, nil
}

func (r mysqlRepo) GetActors(
Expand Down
8 changes: 4 additions & 4 deletions web/handler/subject/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ func parseBrowseQuery(c echo.Context) (filter subject.BrowseFilter, err error) {
}

if yearStr := c.QueryParam("year"); yearStr != "" {
if year, e := gstr.ParseUint16(yearStr); e != nil {
if year, e := gstr.ParseInt32(yearStr); e != nil {
err = res.BadRequest(e.Error())
return
} else {
filter.Year = null.Uint16{Value: year, Set: true}
filter.Year = null.Int32{Value: year, Set: true}
}
}

if monthStr := c.QueryParam("month"); monthStr != "" {
if month, e := gstr.ParseUint8(monthStr); e != nil {
if month, e := gstr.ParseInt8(monthStr); e != nil {
err = res.BadRequest(e.Error())
return
} else {
filter.Month = null.Uint8{Value: month, Set: true}
filter.Month = null.Int8{Value: month, Set: true}
}
}

Expand Down

0 comments on commit 4665e98

Please sign in to comment.