64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package request
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
"web-qudo-be/app/database/entity"
|
|
"web-qudo-be/utils/paginator"
|
|
)
|
|
|
|
type BookmarksGeneric interface {
|
|
ToEntity()
|
|
}
|
|
|
|
type BookmarksQueryRequest struct {
|
|
UserId *uint `json:"userId"`
|
|
ArticleId *uint `json:"articleId"`
|
|
Pagination *paginator.Pagination `json:"pagination"`
|
|
}
|
|
|
|
type BookmarksCreateRequest struct {
|
|
ArticleId uint `json:"articleId" validate:"required"`
|
|
}
|
|
|
|
func (req BookmarksCreateRequest) ToEntity(userId uint) *entity.Bookmarks {
|
|
return &entity.Bookmarks{
|
|
UserId: userId,
|
|
ArticleId: req.ArticleId,
|
|
IsActive: boolPtr(true),
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
type BookmarksQueryRequestContext struct {
|
|
UserId string `json:"userId"`
|
|
ArticleId string `json:"articleId"`
|
|
}
|
|
|
|
func (req BookmarksQueryRequestContext) ToParamRequest() BookmarksQueryRequest {
|
|
var request BookmarksQueryRequest
|
|
|
|
if userIdStr := req.UserId; userIdStr != "" {
|
|
userId, err := strconv.Atoi(userIdStr)
|
|
if err == nil {
|
|
userIdUint := uint(userId)
|
|
request.UserId = &userIdUint
|
|
}
|
|
}
|
|
if articleIdStr := req.ArticleId; articleIdStr != "" {
|
|
articleId, err := strconv.Atoi(articleIdStr)
|
|
if err == nil {
|
|
articleIdUint := uint(articleId)
|
|
request.ArticleId = &articleIdUint
|
|
}
|
|
}
|
|
|
|
return request
|
|
}
|
|
|
|
// Helper function to create bool pointer
|
|
func boolPtr(b bool) *bool {
|
|
return &b
|
|
}
|