Skip to content

Commit

Permalink
feat: add quotation creating filter (can only create once)
Browse files Browse the repository at this point in the history
  • Loading branch information
kastnerorz committed Apr 5, 2020
1 parent 7de4c1f commit a090acf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
21 changes: 19 additions & 2 deletions backend/routers/quotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
)

func CreateQuotation(c *gin.Context) {
o, _ := c.Get(middlewares.IdentityKey)
user := o.(*models.User)
user := tools.GetUserFromContext(c)

var quotation models.Quotation
err := c.BindJSON(&quotation)
Expand All @@ -42,7 +41,25 @@ func CreateQuotation(c *gin.Context) {
return
}

filter := bson.M{}

lowerBound, upperBound := tools.GetValidDateLowerAndUpperBound()
filter["lastModified"] = bson.M{
"$gt": lowerBound,
"$lte": upperBound,
}

filter["author._id"] = user.ID

mongoCtx, collection := pkg.GetMongoContext("quotations")
count, err := collection.CountDocuments(mongoCtx, filter)
if count > 0 {
c.JSON(http.StatusBadRequest, gin.H{"code": -3, "msg": "半(自然)天只能发布一次报价,请在下个变化周期内发布!"})
log.Println(err)
return
}

mongoCtx, collection = pkg.GetMongoContext("quotations")
user.Password = ""
user.SwitchFriendCode = ""
_, err = collection.InsertOne(mongoCtx, bson.M{
Expand Down
10 changes: 5 additions & 5 deletions backend/routers/quotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func TestGetMyQuotation(t *testing.T) {
assert.Equal(t, 0, quotation.HandlingFee)
}

func TestDeleteQuotation(t *testing.T) {
r := PerformRequestWithAuth("DELETE", "/api/v1/quotations/"+quotationId, nil, ReviewerToken)
assert.Equal(t, http.StatusOK, r.Code)
}

func TestCreateQuotationPassCode(t *testing.T) {
body := []byte(`{"type":"SELL","price":40,"openType":"PASS_CODE","passCode":"56HMS","handlingFee":100000}`)
r := PerformRequestWithAuth("POST", "/api/v1/quotations", bytes.NewBuffer(body), ReviewerToken)
Expand All @@ -83,8 +88,3 @@ func TestGetMyQuotationPassCode(t *testing.T) {
assert.Equal(t, "PASS_CODE", quotation.OpenType)
assert.Equal(t, 100000, quotation.HandlingFee)
}

func TestDeleteQuotation(t *testing.T) {
r := PerformRequestWithAuth("DELETE", "/api/v1/quotations/"+quotationId, nil, ReviewerToken)
assert.Equal(t, http.StatusOK, r.Code)
}

0 comments on commit a090acf

Please sign in to comment.