feat: add tags support

This commit is contained in:
Paul Pan 2023-12-27 21:30:52 +08:00
parent f51f4fd3eb
commit ce7d173a82
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4
10 changed files with 47 additions and 24 deletions

View File

@ -7,7 +7,8 @@ import (
)
type searchRequest struct {
Search string `form:"search" json:"search"`
Keyword string `form:"keyword" json:"keyword"`
Tag string `form:"tag" json:"tag"`
}
// Search
@ -16,7 +17,8 @@ type searchRequest struct {
// @Tags problem
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param search formData string false "keyword"
// @Param keyword formData string false "keyword"
// @Param tag formData string false "tag"
// @Response 200 {object} e.Response[[]model.Problem] "problems found"
// @Router /v1/problem/search [post]
func (h *handler) Search(c *gin.Context) {
@ -27,13 +29,13 @@ func (h *handler) Search(c *gin.Context) {
}
// TODO: pagination
if req.Search == "" {
if req.Keyword == "" {
// TODO: query without LIKE
problems, status := h.problemService.QueryFuzz(req.Search, true, true)
problems, status := h.problemService.QueryFuzz(req.Keyword, req.Tag, true, true)
e.Pong(c, status, problems)
return
} else {
problems, status := h.problemService.QueryFuzz(req.Search, true, true)
problems, status := h.problemService.QueryFuzz(req.Keyword, req.Tag, true, true)
e.Pong(c, status, problems)
return
}

View File

@ -6,12 +6,14 @@ import (
"git.0x7f.app/WOJ/woj-server/internal/service/problem"
"git.0x7f.app/WOJ/woj-server/pkg/utils"
"github.com/gin-gonic/gin"
"github.com/jackc/pgtype"
)
type updateRequest struct {
Pid uint `form:"pid" json:"pid"`
Title string `form:"title" json:"title"`
Statement string `form:"statement" json:"statement"`
Tags []string `form:"tags" json:"tags"`
IsEnabled bool `form:"is_enabled" json:"is_enabled"`
}
@ -24,6 +26,7 @@ type updateRequest struct {
// @Param pid formData int false "problem id, 0 for create"
// @Param title formData string false "title"
// @Param statement formData string false "statement"
// @Param tags formData []string false "tags"
// @Param is_enabled formData bool false "is enabled"
// @Response 200 {object} e.Response[model.Problem] "problem info without provider information"
// @Security Authentication
@ -60,6 +63,7 @@ func (h *handler) Update(c *gin.Context) {
createData := &problem.CreateData{
Title: req.Title,
Statement: req.Statement,
Tags: req.Tags,
ProviderID: uid,
IsEnabled: false,
}
@ -83,6 +87,11 @@ func (h *handler) Update(c *gin.Context) {
// update problem
p.Title = utils.If(req.Title != "", req.Title, p.Title)
p.Statement = utils.If(req.Statement != "", req.Statement, p.Statement)
if len(req.Tags) != 0 {
tags := pgtype.TextArray{}
_ = tags.Set(req.Tags)
p.Tags = tags
}
p.IsEnabled = req.IsEnabled
p, status = h.problemService.Update(p)

View File

@ -7,8 +7,9 @@ import (
type Problem struct {
gorm.Model `json:"meta"`
Title string `json:"title" gorm:"not null"`
Title string `json:"title" gorm:"not null;index"`
Statement string `json:"statement" gorm:"not null"`
Tags pgtype.TextArray `json:"tags" gorm:"type:text[];index"`
ProviderID uint `json:"-" gorm:"not null;index"`
Provider User `json:"provider" gorm:"foreignKey:ProviderID"`
IsEnabled bool `json:"is_enabled" gorm:"not null;index"`

View File

@ -3,20 +3,25 @@ package problem
import (
"git.0x7f.app/WOJ/woj-server/internal/e"
"git.0x7f.app/WOJ/woj-server/internal/model"
"github.com/jackc/pgtype"
"go.uber.org/zap"
)
type CreateData struct {
Title string
Statement string
Tags []string
ProviderID uint
IsEnabled bool
}
func (s *service) Create(data *CreateData) (*model.Problem, e.Status) {
tags := pgtype.TextArray{}
_ = tags.Set(data.Tags)
problem := &model.Problem{
Title: data.Title,
Statement: data.Statement,
Tags: tags,
ProviderID: data.ProviderID,
IsEnabled: data.IsEnabled,
}

View File

@ -7,22 +7,28 @@ import (
"gorm.io/gorm/clause"
)
func (s *service) QueryFuzz(search string, associations bool, shouldEnable bool) ([]*model.Problem, e.Status) {
func (s *service) QueryFuzz(keyword string, tag string, associations bool, shouldEnable bool) ([]*model.Problem, e.Status) {
problems := make([]*model.Problem, 0)
query := s.db.Get()
if associations {
query = query.Preload(clause.Associations)
}
if shouldEnable {
query = query.Where("is_enabled = true")
}
query = query.
Where(s.db.Get().Where("title LIKE ?", "%"+search+"%").
Or("statement LIKE ?", "%"+search+"%"))
Where(s.db.Get().Where("title LIKE ?", "%"+keyword+"%").
Or("statement LIKE ?", "%"+keyword+"%"))
if tag != "" {
query = query.Where("tags @> array[?]", tag)
}
err := query.Find(&problems).Error
if err != nil {
s.log.Warn("DatabaseError", zap.Error(err), zap.Any("search", search))
s.log.Warn("DatabaseError", zap.Error(err), zap.Any("keyword", keyword))
return nil, e.DatabaseError
}

View File

@ -15,7 +15,7 @@ type Service interface {
Create(data *CreateData) (*model.Problem, e.Status)
Update(problem *model.Problem) (*model.Problem, e.Status)
Query(pid uint, associations bool, shouldEnable bool) (*model.Problem, e.Status)
QueryFuzz(search string, associations bool, shouldEnable bool) ([]*model.Problem, e.Status)
QueryFuzz(keyword string, tag string, associations bool, shouldEnable bool) ([]*model.Problem, e.Status)
CreateVersion(data *CreateVersionData) (*model.ProblemVersion, e.Status)
UpdateVersion(pvid uint, values interface{}) e.Status