222 lines
6.6 KiB
Go
222 lines
6.6 KiB
Go
package officialwebsiteser
|
|
|
|
import (
|
|
"time"
|
|
|
|
"91porn-server/common/stderr"
|
|
officialwebsitemod "91porn-server/models/v/officialWebsitemod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// ─────────────────────────────────────────
|
|
// Service functions
|
|
// ─────────────────────────────────────────
|
|
|
|
func ListRecruitForm(req *RecruitFormListReq) (int64, []officialwebsitemod.RecruitForm, error) {
|
|
filter, err := req.Query()
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
mod := &officialwebsitemod.RecruitForm{}
|
|
total, err := mod.Count(filter)
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
list, err := mod.FindMany(filter, req.FindOptions())
|
|
if err != nil {
|
|
return 0, nil, err
|
|
}
|
|
return total, list, nil
|
|
}
|
|
|
|
func CreateRecruitForm(req *ModifyRecruitFormReq) (officialwebsitemod.RecruitForm, error) {
|
|
data := req.toMod()
|
|
if err := data.Create(); err != nil {
|
|
return officialwebsitemod.RecruitForm{}, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func UpdateRecruitForm(req *ModifyRecruitFormReq) (resp CountResp, err error) {
|
|
if req == nil || req.ID == "" {
|
|
return
|
|
}
|
|
id, parseErr := primitive.ObjectIDFromHex(req.ID)
|
|
if parseErr != nil || isNilObjectID(id) {
|
|
err = stderr.ErrParamError
|
|
return
|
|
}
|
|
mod := &officialwebsitemod.RecruitForm{}
|
|
if err = mod.FindOne(officialwebsitemod.M{"_id": id}); err != nil {
|
|
return
|
|
}
|
|
var update officialwebsitemod.M
|
|
if update, err = req.toM(); err != nil || update == nil {
|
|
return
|
|
}
|
|
resp.Count, err = mod.Update(officialwebsitemod.M{"_id": id}, update)
|
|
return
|
|
}
|
|
|
|
func DeleteRecruitForm(req *DeleteRecruitFormReq) (data DeleteRecruitFormResp, err error) {
|
|
if req == nil {
|
|
err = stderr.CodeEmptyData
|
|
return
|
|
}
|
|
var id primitive.ObjectID
|
|
if id, err = primitive.ObjectIDFromHex(req.ID); err != nil {
|
|
return
|
|
}
|
|
if isNilObjectID(id) {
|
|
err = stderr.ErrParamError
|
|
return
|
|
}
|
|
mod := &officialwebsitemod.RecruitForm{}
|
|
data.Count, err = mod.Delete(officialwebsitemod.M{"_id": id})
|
|
return
|
|
}
|
|
|
|
// ─────────────────────────────────────────
|
|
// Mutation (Modify) types
|
|
// ─────────────────────────────────────────
|
|
|
|
type ModifyRecruitFormReq struct {
|
|
ID string `json:"id" form:"id"`
|
|
Name *string `json:"name"`
|
|
Sex *int `json:"sex"`
|
|
Age *int `json:"age"`
|
|
Country *string `json:"country"`
|
|
Address *string `json:"address"`
|
|
Contact *[]officialwebsitemod.ContactInfo `json:"contact"`
|
|
Description *string `json:"description"`
|
|
}
|
|
|
|
// ─────────────────────────────────────────
|
|
// Delete types
|
|
// ─────────────────────────────────────────
|
|
|
|
type DeleteRecruitFormReq struct {
|
|
ID string `json:"id" form:"id"`
|
|
}
|
|
|
|
type DeleteRecruitFormResp struct {
|
|
Count int64 `json:"count"`
|
|
}
|
|
|
|
// ─────────────────────────────────────────
|
|
// toMod — ModifyRecruitFormReq → model struct
|
|
// ─────────────────────────────────────────
|
|
|
|
func (req *ModifyRecruitFormReq) toMod() officialwebsitemod.RecruitForm {
|
|
if req == nil {
|
|
return officialwebsitemod.RecruitForm{}
|
|
}
|
|
data := officialwebsitemod.RecruitForm{
|
|
Name: stringOrZero(req.Name),
|
|
Sex: intOrZero(req.Sex),
|
|
Age: intOrZero(req.Age),
|
|
Country: stringOrZero(req.Country),
|
|
Address: stringOrZero(req.Address),
|
|
Description: stringOrZero(req.Description),
|
|
}
|
|
if req.Contact != nil {
|
|
data.Contact = *req.Contact
|
|
}
|
|
return data
|
|
}
|
|
|
|
// ─────────────────────────────────────────
|
|
// toM — ModifyRecruitFormReq → officialwebsitemod.M
|
|
// ─────────────────────────────────────────
|
|
|
|
func (req *ModifyRecruitFormReq) toM() (officialwebsitemod.M, error) {
|
|
if req == nil {
|
|
return nil, stderr.CodeEmptyData
|
|
}
|
|
set := officialwebsitemod.M{}
|
|
if req.Name != nil {
|
|
set["name"] = *req.Name
|
|
}
|
|
if req.Sex != nil {
|
|
set["sex"] = *req.Sex
|
|
}
|
|
if req.Age != nil {
|
|
set["age"] = *req.Age
|
|
}
|
|
if req.Country != nil {
|
|
set["country"] = *req.Country
|
|
}
|
|
if req.Address != nil {
|
|
set["address"] = *req.Address
|
|
}
|
|
if req.Contact != nil {
|
|
set["contact"] = *req.Contact
|
|
}
|
|
if req.Description != nil {
|
|
set["description"] = *req.Description
|
|
}
|
|
if len(set) == 0 {
|
|
return nil, nil
|
|
}
|
|
set["updatedAt"] = time.Now().UTC()
|
|
return officialwebsitemod.M{"$set": set}, nil
|
|
}
|
|
|
|
// ─────────────────────────────────────────
|
|
// List types
|
|
// ─────────────────────────────────────────
|
|
|
|
type RecruitFormListReq struct {
|
|
PageReq
|
|
ID *string `form:"id"`
|
|
Name *string `form:"name"`
|
|
Sex *int `form:"sex"`
|
|
Age *int `form:"age"`
|
|
Country *string `form:"country"`
|
|
Address *string `form:"address"`
|
|
Contact *string `form:"contact"`
|
|
Description *string `form:"description"`
|
|
CreatedAt *time.Time `form:"createdAt"`
|
|
UpdatedAt *time.Time `form:"updatedAt"`
|
|
DeletedAt *time.Time `form:"deletedAt"`
|
|
}
|
|
|
|
func (r *RecruitFormListReq) Query() (officialwebsitemod.M, error) {
|
|
filter := officialwebsitemod.M{}
|
|
if err := parseObjectIDFilter(filter, "_id", r.ID); err != nil {
|
|
return nil, err
|
|
}
|
|
if r.Name != nil {
|
|
filter["name"] = *r.Name
|
|
}
|
|
if r.Sex != nil {
|
|
filter["sex"] = *r.Sex
|
|
}
|
|
if r.Age != nil {
|
|
filter["age"] = *r.Age
|
|
}
|
|
if r.Country != nil {
|
|
filter["country"] = *r.Country
|
|
}
|
|
if r.Address != nil {
|
|
filter["address"] = *r.Address
|
|
}
|
|
if err := parseJSONFilter(filter, "contact", r.Contact, &[]officialwebsitemod.ContactInfo{}); err != nil {
|
|
return nil, err
|
|
}
|
|
if r.Description != nil {
|
|
filter["description"] = *r.Description
|
|
}
|
|
if r.CreatedAt != nil {
|
|
filter["createdAt"] = *r.CreatedAt
|
|
}
|
|
if r.UpdatedAt != nil {
|
|
filter["updatedAt"] = *r.UpdatedAt
|
|
}
|
|
if r.DeletedAt != nil {
|
|
filter["deletedAt"] = *r.DeletedAt
|
|
}
|
|
return filter, nil
|
|
}
|