diff --git a/internal/pkg/gstr/parse.go b/internal/pkg/gstr/parse.go index 7cb737bab..84287ed13 100644 --- a/internal/pkg/gstr/parse.go +++ b/internal/pkg/gstr/parse.go @@ -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) @@ -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) diff --git a/internal/subject/domain.go b/internal/subject/domain.go index d0b78d0c9..c92e94e60 100644 --- a/internal/subject/domain.go +++ b/internal/subject/domain.go @@ -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 { diff --git a/internal/subject/mysql_repository.go b/internal/subject/mysql_repository.go index fd21932b9..5218dd0ca 100644 --- a/internal/subject/mysql_repository.go +++ b/internal/subject/mysql_repository.go @@ -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( diff --git a/web/handler/subject/browse.go b/web/handler/subject/browse.go index 2f302a037..ffaafdfa0 100644 --- a/web/handler/subject/browse.go +++ b/web/handler/subject/browse.go @@ -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} } }