Skip to content

Commit

Permalink
Feature: Automessages now have lower threshold limits
Browse files Browse the repository at this point in the history
  • Loading branch information
khades committed Jan 13, 2018
1 parent 0d3481f commit 10ffedc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 11 additions & 3 deletions httpbackend/autoMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ func autoMessageCreate(w http.ResponseWriter, r *http.Request, s *models.HTTPSes
return

}
id := repos.CreateAutoMessage(&request)

id, validationError := repos.CreateAutoMessage(&request)
if validationError != nil {
writeJSONError(w, "Validation Failed", http.StatusUnprocessableEntity)
return
}
json.NewEncoder(w).Encode(autoMessageCreationResponse{*id})

}
Expand Down Expand Up @@ -95,5 +98,10 @@ func autoMessageUpdate(w http.ResponseWriter, r *http.Request, s *models.HTTPSes
request.UserID = s.UserID
request.ChannelID = *channelID
request.ID = id
repos.UpdateAutoMessage(&request)
validationError := repos.UpdateAutoMessage(&request)
if validationError != nil {
writeJSONError(w, "Validation Failed", http.StatusUnprocessableEntity)
return
}

}
15 changes: 11 additions & 4 deletions repos/autoMessage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repos

import (
"errors"
"log"
"time"

Expand Down Expand Up @@ -57,9 +58,12 @@ func GetAutoMessages(channelID *string) (*[]models.AutoMessage, error) {
error := Db.C(autoMessageCollectionName).Find(bson.M{"channelid": *channelID}).All(&result)
return &result, error
}
func CreateAutoMessage(autoMessageUpdate *models.AutoMessageUpdate) *bson.ObjectId {
func CreateAutoMessage(autoMessageUpdate *models.AutoMessageUpdate) (*bson.ObjectId, error) {
id := bson.NewObjectId()
now := time.Now()
if autoMessageUpdate.DurationLimit < 60 || autoMessageUpdate.MessageLimit < 20 {
return nil, errors.New("Validation Failed")
}
var durationLimit = time.Second * time.Duration(autoMessageUpdate.DurationLimit)
Db.C(autoMessageCollectionName).Insert(
models.AutoMessageWithHistory{
Expand All @@ -81,10 +85,13 @@ func CreateAutoMessage(autoMessageUpdate *models.AutoMessageUpdate) *bson.Object
Message: autoMessageUpdate.Message,
MessageLimit: autoMessageUpdate.MessageLimit,
DurationLimit: durationLimit}}})
return &id
return &id, nil
}

func UpdateAutoMessage(autoMessageUpdate *models.AutoMessageUpdate) {
func UpdateAutoMessage(autoMessageUpdate *models.AutoMessageUpdate) error {
if autoMessageUpdate.DurationLimit < 60 || autoMessageUpdate.MessageLimit < 20 {
return errors.New("Validation Failed")
}
now := time.Now()
var durationLimit = time.Second * time.Duration(autoMessageUpdate.DurationLimit)
Db.C(autoMessageCollectionName).Update(
Expand All @@ -111,5 +118,5 @@ func UpdateAutoMessage(autoMessageUpdate *models.AutoMessageUpdate) {
Game: autoMessageUpdate.Game,
DurationLimit: durationLimit,
DurationThreshold: now.Add(durationLimit)}})

return nil
}

0 comments on commit 10ffedc

Please sign in to comment.