Skip to content

Commit

Permalink
feat: add Application
Browse files Browse the repository at this point in the history
  • Loading branch information
kastnerorz committed Mar 31, 2020
1 parent c66868d commit c889c5f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 41 deletions.
1 change: 1 addition & 0 deletions service/application_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
91 changes: 69 additions & 22 deletions service/quotation_controller.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -28,32 +29,43 @@ 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
}

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{}

Expand All @@ -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": "报价查询失败!"})
Expand Down Expand Up @@ -142,36 +167,58 @@ func GetMyQuotation(c *gin.Context) {
}

func UpdateQuotation(c *gin.Context) {
o, _ := c.Get(IdentityKey)
user := o.(*User)

var param QuotationParam
err := c.BindJSON(&param)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": -1, "msg": "内部错误!"})
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
mongoCtx, collection := GetMongoContext("quotations")
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(&quotation)
if err != nil {
err = collection.FindOneAndUpdate(mongoCtx, bson.M{"_id": objectId, "author._id": user.ID}, bson.M{"$set": set}, opt).Decode(&quotation)
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)
}
60 changes: 44 additions & 16 deletions service/types.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,67 @@
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{}{
"SELL": {},
"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"`
Expand Down
11 changes: 8 additions & 3 deletions service/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)内部错误!"})
Expand All @@ -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)
}
}

0 comments on commit c889c5f

Please sign in to comment.