-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from Receipt-Wrangler/tech/update-receipt-upd…
…ate-to-use-command Tech/update receipt update to use command
- Loading branch information
Showing
20 changed files
with
740 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
package commands | ||
|
||
import "receipt-wrangler/api/internal/models" | ||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"receipt-wrangler/api/internal/models" | ||
"receipt-wrangler/api/internal/utils" | ||
) | ||
|
||
type BulkStatusUpdateCommand struct { | ||
Comment string | ||
Status models.ReceiptStatus | ||
ReceiptIds []uint | ||
} | ||
|
||
func (command *BulkStatusUpdateCommand) LoadDataFromRequest(w http.ResponseWriter, r *http.Request) error { | ||
bytes, err := utils.GetBodyData(w, r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(bytes, &command) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"receipt-wrangler/api/internal/structs" | ||
"receipt-wrangler/api/internal/utils" | ||
) | ||
|
||
type UpsertCategoryCommand struct { | ||
Id *uint `json:"id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
} | ||
|
||
func (category *UpsertCategoryCommand) LoadDataFromRequest(w http.ResponseWriter, r *http.Request) error { | ||
bytes, err := utils.GetBodyData(w, r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(bytes, &category) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (category *UpsertCategoryCommand) Validate() structs.ValidatorError { | ||
errors := make(map[string]string) | ||
vErr := structs.ValidatorError{} | ||
|
||
if len(category.Name) == 0 { | ||
errors["name"] = "Name is required" | ||
} | ||
|
||
vErr.Errors = errors | ||
return vErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package commands | ||
|
||
import ( | ||
"receipt-wrangler/api/internal/structs" | ||
) | ||
|
||
type UpsertCommentCommand struct { | ||
Comment string `json:"comment"` | ||
ReceiptId uint `json:"receiptId"` | ||
UserId uint `json:"userId"` | ||
} | ||
|
||
func (comment *UpsertCommentCommand) Validate(userRequestId uint, isCreate bool) structs.ValidatorError { | ||
errors := make(map[string]string) | ||
vErr := structs.ValidatorError{} | ||
|
||
if len(comment.Comment) == 0 { | ||
errors["comment"] = "Comment is required" | ||
} | ||
|
||
if !isCreate { | ||
if comment.ReceiptId == 0 { | ||
errors["receiptId"] = "Receipt Id is required" | ||
} | ||
} | ||
|
||
if comment.UserId == 0 { | ||
errors["userId"] = "User Id is required" | ||
} | ||
|
||
if comment.UserId != userRequestId { | ||
errors["userId"] = "Bad user id" | ||
} | ||
|
||
vErr.Errors = errors | ||
return vErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/shopspring/decimal" | ||
"receipt-wrangler/api/internal/models" | ||
"receipt-wrangler/api/internal/structs" | ||
) | ||
|
||
type UpsertItemCommand struct { | ||
Amount decimal.Decimal `json:"amount"` | ||
ChargedToUserId uint `json:"chargedToUserId"` | ||
IsTaxed bool `json:"isTaxed"` | ||
Name string `json:"name"` | ||
ReceiptId uint `json:"receiptId"` | ||
Status models.ItemStatus `json:"status"` | ||
} | ||
|
||
func (item *UpsertItemCommand) Validate(receiptAmount decimal.Decimal, isCreate bool) structs.ValidatorError { | ||
errors := make(map[string]string) | ||
vErr := structs.ValidatorError{} | ||
|
||
if item.Amount.IsZero() { | ||
errors["amount"] = "Amount is required" | ||
} | ||
|
||
if item.Amount.GreaterThan(receiptAmount) { | ||
errors["amount"] = "Amount cannot be greater than receipt amount" | ||
} | ||
|
||
if item.Amount.LessThanOrEqual(decimal.Zero) { | ||
errors["amount"] = "Amount must be greater than zero" | ||
} | ||
|
||
if len(item.Name) == 0 { | ||
errors["name"] = "Name is required" | ||
} | ||
|
||
if !isCreate { | ||
if item.ReceiptId == 0 { | ||
errors["receiptId"] = "Receipt Id is required" | ||
} | ||
} | ||
|
||
if item.ChargedToUserId == 0 { | ||
errors["chargedToUserId"] = "Charged To User Id is required" | ||
} | ||
|
||
if len(item.Status) == 0 { | ||
errors["status"] = "Status is required" | ||
} | ||
|
||
vErr.Errors = errors | ||
return vErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/shopspring/decimal" | ||
"net/http" | ||
"receipt-wrangler/api/internal/models" | ||
"receipt-wrangler/api/internal/structs" | ||
"receipt-wrangler/api/internal/utils" | ||
"time" | ||
) | ||
|
||
type UpsertReceiptCommand struct { | ||
Name string `json:"name"` | ||
Amount decimal.Decimal `json:"amount"` | ||
Date time.Time `json:"date"` | ||
GroupId uint `json:"groupId"` | ||
PaidByUserID uint `json:"paidByUserId"` | ||
Status models.ReceiptStatus `json:"status"` | ||
Categories []UpsertCategoryCommand `json:"categories"` | ||
Tags []UpsertTagCommand `json:"tags"` | ||
Items []UpsertItemCommand `json:"receiptItems"` | ||
Comments []UpsertCommentCommand `json:"comments"` | ||
CreatedByString string `json:"createdByString"` | ||
} | ||
|
||
func (receipt *UpsertReceiptCommand) LoadDataFromRequest(w http.ResponseWriter, r *http.Request) error { | ||
bytes, err := utils.GetBodyData(w, r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(bytes, &receipt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (receipt *UpsertReceiptCommand) Validate(tokenUserId uint, isCreate bool) structs.ValidatorError { | ||
errors := make(map[string]string) | ||
vErr := structs.ValidatorError{} | ||
|
||
if len(receipt.Name) == 0 { | ||
errors["name"] = "Name is required" | ||
} | ||
|
||
if receipt.Amount.IsZero() { | ||
errors["amount"] = "Amount is required" | ||
} | ||
|
||
if receipt.Amount.LessThanOrEqual(decimal.Zero) { | ||
errors["amount"] = "Amount must be greater than zero" | ||
} | ||
|
||
if receipt.Date.IsZero() { | ||
errors["date"] = "Date is required" | ||
} | ||
|
||
if receipt.GroupId == 0 { | ||
errors["groupId"] = "Group Id is required" | ||
} | ||
|
||
if receipt.PaidByUserID == 0 { | ||
errors["paidByUserId"] = "Paid By User Id is required" | ||
} | ||
|
||
if receipt.Status == "" { | ||
errors["status"] = "Status is required" | ||
} | ||
|
||
for i, category := range receipt.Categories { | ||
basePath := "categories." + fmt.Sprintf("%d", i) | ||
categoryErrors := category.Validate() | ||
for key, value := range categoryErrors.Errors { | ||
errors[basePath+"."+key] = value | ||
} | ||
} | ||
|
||
for i, tag := range receipt.Tags { | ||
basePath := "tags." + fmt.Sprintf("%d", i) | ||
tagErrors := tag.Validate() | ||
for key, value := range tagErrors.Errors { | ||
errors[basePath+"."+key] = value | ||
} | ||
} | ||
|
||
for i, item := range receipt.Items { | ||
basePath := "receiptItems." + fmt.Sprintf("%d", i) | ||
itemErrors := item.Validate(receipt.Amount, isCreate) | ||
for key, value := range itemErrors.Errors { | ||
errors[basePath+"."+key] = value | ||
} | ||
} | ||
|
||
for i, comment := range receipt.Comments { | ||
basePath := "comments." + fmt.Sprintf("%d", i) | ||
commentErrors := comment.Validate(tokenUserId, isCreate) | ||
for key, value := range commentErrors.Errors { | ||
errors[basePath+"."+key] = value | ||
} | ||
} | ||
|
||
vErr.Errors = errors | ||
return vErr | ||
} | ||
|
||
func (receipt *UpsertReceiptCommand) ToReceipt() (models.Receipt, error) { | ||
var result models.Receipt | ||
bytes, err := json.Marshal(receipt) | ||
if err != nil { | ||
return result, err | ||
} | ||
|
||
err = json.Unmarshal(bytes, &result) | ||
if err != nil { | ||
return result, err | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.