diff --git a/service/application_controller.go b/service/application_controller.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/service/application_controller.go @@ -0,0 +1 @@ +package main diff --git a/service/quotation_controller.go b/service/quotation_controller.go index 8841629..61fc084 100644 --- a/service/quotation_controller.go +++ b/service/quotation_controller.go @@ -1,13 +1,14 @@ package main import ( + "fmt" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "log" "net/http" - "strconv" "time" ) @@ -28,6 +29,11 @@ func CreateQuotation(c *gin.Context) { return } + if _, ok := OpenType[quotation.OpenType]; !ok { + c.JSON(http.StatusBadRequest, gin.H{"code": -2, "msg": "岛屿开放类型不正确!"}) + return + } + if quotation.Price == 0 { c.JSON(http.StatusBadRequest, gin.H{"code": -3, "msg": "报价不合法!"}) return @@ -35,25 +41,31 @@ func CreateQuotation(c *gin.Context) { mongoCtx, collection := GetMongoContext("quotations") user.Password = "" + user.SwitchFriendCode = "" + user.Username = "" _, err = collection.InsertOne(mongoCtx, bson.M{ - "type": quotation.Type, - "author": user, - "price": quotation.Price, - "participantCount": 0, - "verified": false, - "lastModified": time.Now(), + "type": quotation.Type, + "author": user, + "price": quotation.Price, + "validCount": 0, + "invalidCount": 0, + "openType": quotation.OpenType, + "passCode": quotation.PassCode, + "handlingFee": quotation.HandlingFee, + "lastModified": time.Now(), }) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"code": -3, "msg": "Error while inserting into database."}) log.Println(err) return } + c.Status(http.StatusCreated) } func GetQuotations(c *gin.Context) { quotationType := c.Query("type") - verified := c.Query("verified") - available := c.Query("available") + openType := c.Query("openType") + //isValid := c.Query("isValid") filter := bson.M{} @@ -66,23 +78,36 @@ func GetQuotations(c *gin.Context) { filter["type"] = quotationType } - if verified != "" { - filter["verified"], _ = strconv.ParseBool(verified) + if openType != "" { + if _, ok := OpenType[openType]; !ok { + c.JSON(http.StatusOK, []struct{}{}) + return + } + filter["openType"] = openType } - if available != "" { - filter["available"], _ = strconv.ParseBool(available) - } + //if isValid != "" { + // v, _ := strconv.ParseBool(isValid); + // if v { + // filter["$where"] = "this.validCount > this.inValidCount" + // } else { + // filter["$where"] = "this.validCount < this.inValidCount" + // } + //} lowerBound, upperBound := GetValidDateLowerAndUpperBound() filter["lastModified"] = bson.M{ "$gt": lowerBound, "$lte": upperBound, } + fmt.Print(filter) mongoCtx, collection := GetMongoContext("quotations") opts := options.Find() opts.SetSort(bson.D{{"price", -1}}) opts.SetLimit(10) + opts.SetProjection(bson.D{ + {"passCode", 0}, + }) cursor, err := collection.Find(mongoCtx, filter, opts) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"code": -1, "msg": "报价查询失败!"}) @@ -142,6 +167,9 @@ func GetMyQuotation(c *gin.Context) { } func UpdateQuotation(c *gin.Context) { + o, _ := c.Get(IdentityKey) + user := o.(*User) + var param QuotationParam err := c.BindJSON(¶m) if err != nil { @@ -149,17 +177,31 @@ func UpdateQuotation(c *gin.Context) { log.Println(err) return } - participantCount := param.ParticipantCount - verified := param.Verified + price := param.Price + openType := param.OpenType + passCode := param.PassCode + handlingFee := param.HandlingFee set := bson.M{} - if participantCount != nil { - set["participantCount"] = participantCount + if price != nil { + set["price"] = price } - if verified != nil { - set["verified"] = verified + if openType != "" { + if _, ok := OpenType[openType]; !ok { + c.JSON(http.StatusBadRequest, gin.H{"code": -2, "msg": "岛屿开放类型不正确!"}) + return + } + set["openType"] = openType + } + + if passCode != "" { + set["passCode"] = passCode + } + + if handlingFee != nil { + set["handlingFee"] = handlingFee } var quotation Quotation @@ -167,11 +209,16 @@ func UpdateQuotation(c *gin.Context) { objectId, _ := primitive.ObjectIDFromHex(c.Param("id")) opt := options.FindOneAndUpdate() opt.SetReturnDocument(options.After) - err = collection.FindOneAndUpdate(mongoCtx, bson.M{"_id": objectId}, bson.M{"$set": set}, opt).Decode("ation) - if err != nil { + err = collection.FindOneAndUpdate(mongoCtx, bson.M{"_id": objectId, "author._id": user.ID}, bson.M{"$set": set}, opt).Decode("ation) + if err != nil && err != mongo.ErrNoDocuments { c.JSON(http.StatusInternalServerError, gin.H{"code": -1, "msg": "更新报价信息失败!"}) log.Println(err) return } + + if err == mongo.ErrNoDocuments { + c.JSON(http.StatusForbidden, gin.H{"code": -1, "msg": "没有这个报价信息或无权限更改!"}) + return + } c.JSON(http.StatusOK, quotation) } diff --git a/service/types.go b/service/types.go index 7f53a60..f62153a 100644 --- a/service/types.go +++ b/service/types.go @@ -1,32 +1,41 @@ package main -import "time" +import ( + "go.mongodb.org/mongo-driver/bson/primitive" + "time" +) type User struct { ID string `json:"id" bson:"_id"` - Username string `json:"username" bson:"username"` + Username string `json:"username,omitempty" bson:"username"` Nickname string `json:"nickname" bson:"nickname"` Password string `json:"password,omitempty" bson:"password"` - SwitchFriendCode string `json:"switchFriendCode" bson:"switchFriendCode"` + SwitchFriendCode string `json:"switchFriendCode,omitempty" bson:"switchFriendCode"` + JikeID string `json:"jikeId,omitempty" bson:"jikeId"` } type Quotation struct { - ID string `json:"id" bson:"_id"` - Type string `json:"type" bson:"type"` - Price int `json:"price" bson:"price"` - Author User `json:"author" bson:"author"` - ParticipantCount int `json:"participantCount" bson:"participantCount"` - Verified bool `json:"verified" bson:"verified"` - LastModified time.Time `json:"lastModified" bson:"lastModified"` + ID string `json:"id" bson:"_id"` + Type string `json:"type" bson:"type"` + Price int `json:"price" bson:"price"` + Author User `json:"author" bson:"author"` + ValidCount int `json:"validCount" bson:"validCount"` + InvalidCount int `json:"invalidCount" bson:"invalidCount"` + OpenType string `json:"openType" bson:"openType"` + PassCode string `json:"passCode,omitempty" bson:"passCode"` + HandlingFee int `json:"handlingFee" bson:"handlingFee"` + LastModified time.Time `json:"lastModified" bson:"lastModified"` } type QuotationParam struct { - ID string `json:"id" bson:"_id"` - Type string `json:"type" bson:"type"` - Price *int `json:"price" bson:"price"` - ParticipantCount *int `json:"participantCount" bson:"participantCount"` - Verified *bool `json:"verified" bson:"verified"` - LastModified time.Time `json:"lastModified" bson:"lastModified"` + ID string `json:"id" bson:"_id"` + Type string `json:"type" bson:"type"` + Price *int `json:"price" bson:"price"` + IsValid *bool `json:"isValid" bson:"isValid"` + OpenType string `json:"openType" bson:"openType"` + PassCode string `json:"passCode" bson:"passCode"` + HandlingFee *int `json:"handlingFee" bson:"handlingFee"` + LastModified time.Time `json:"lastModified" bson:"lastModified"` } var QuotationType = map[string]struct{}{ @@ -34,6 +43,25 @@ var QuotationType = map[string]struct{}{ "BUY": {}, } +var OpenType = map[string]struct{}{ + "PASS_CODE": {}, + "FRIENDS": {}, +} + +type Application struct { + Applicant User `json:"applicant" bson:"applicant"` + Reviewer primitive.ObjectID `json:"reviewer" bson:"reviewer"` + PassCode string `json:"passCode" bson:"passCode"` + SwitchFriendCode string `json:"switchFriendCode" bson:"switchFriendCode"` + Status string `json:"status" bson:"status"` +} + +var ApplicationStatus = map[string]struct{}{ + "PENDING": {}, + "ACCEPT": {}, + "REJECT": {}, +} + type Credentials struct { Username string `form:"username" json:"username" binding:"required"` Password string `form:"password" json:"password" binding:"required"` diff --git a/service/user_controller.go b/service/user_controller.go index b1a86a1..2d8d6db 100644 --- a/service/user_controller.go +++ b/service/user_controller.go @@ -66,6 +66,7 @@ func CreateUser(c *gin.Context) { "password": hex.EncodeToString(h.Sum(nil)), "nickname": user.Nickname, "switchFriendCode": user.SwitchFriendCode, + "jikeId": user.JikeID, }) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"code": -8, "msg": "(-8)内部错误!"}) @@ -85,13 +86,17 @@ func GetUser(c *gin.Context) { {"_id", 1}, {"username", 1}, {"nickname", 1}, - {"switchFriendCode", 1}, + {"jikeId", 1}, }) err := collection.FindOne(mongoCtx, bson.M{"_id": objectId}, opt).Decode(&res) - if err != nil { + if err != nil && err != mongo.ErrNoDocuments { c.JSON(http.StatusInternalServerError, gin.H{"code": -1, "msg": "(-1)内部错误"}) log.Println(err) return } - c.JSON(http.StatusOK, res) + if res.ID == "" { + c.JSON(http.StatusNotFound, struct{}{}) + } else { + c.JSON(http.StatusOK, res) + } }