Skip to content

Commit

Permalink
export update && test add
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMaster482 committed Dec 27, 2023
1 parent 280e9fb commit 5614a01
Show file tree
Hide file tree
Showing 4 changed files with 1,264 additions and 1,222 deletions.
31 changes: 27 additions & 4 deletions internal/microservices/IOhub/delivery/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
ShowIncome: gtag.ShowIncome,
ShowOutcome: gtag.ShowOutcome,
Regular: gtag.Regular,
Image: gtag.Image,
}
tags[i] = tag
}
Expand All @@ -237,12 +238,13 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
tagCache := sync.Map{}
if len(tags) != 0 {
for _, tag := range tags {
accountCache.Store(tag.Name, tag.ID)
tagCache.Store(tag.Name, tag.ID)
}
}

var i int
// Iterate through the records
for {
for i = 0; true; i++ {
// Read each record from csv
record, err := reader.Read()
if err == io.EOF {
Expand All @@ -253,18 +255,33 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
return
}

if len(record) < 7 {
response.ErrorResponse(w, http.StatusBadRequest, err, "wrong format not enough args for transaction", h.logger)
return
}

if len(record) > 8 {
response.ErrorResponse(w, http.StatusBadRequest, err, "wrong format too much args for transaction", h.logger)
return
}

accountIncome := record[0]
accountOutcome := record[1]

income, err := strconv.ParseFloat(record[2], 64)
if err != nil {
response.ErrorResponse(w, http.StatusBadRequest, err, "Error converting the amount to float", h.logger)
response.ErrorResponse(w, http.StatusBadRequest, err, "Error converting the income amount to float", h.logger)
return
}

outcome, err := strconv.ParseFloat(record[3], 64)
if err != nil {
response.ErrorResponse(w, http.StatusBadRequest, err, "Error converting the amount to float", h.logger)
response.ErrorResponse(w, http.StatusBadRequest, err, "Error converting the outcome amount to float", h.logger)
return
}

if (income == 0) == (outcome == 0) {
response.ErrorResponse(w, http.StatusBadRequest, err, "transaction can be consumables or profitable", h.logger)
return
}

Expand Down Expand Up @@ -324,6 +341,7 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
tag, err := h.tagService.CreateTag(r.Context(), &generated.CreateTagRequest{
UserId: user.ID.String(),
Name: tagName,
Image: 0,
ShowIncome: false,
Regular: false,
ShowOutcome: true,
Expand Down Expand Up @@ -362,5 +380,10 @@ func (h *Handler) ImportTransactions(w http.ResponseWriter, r *http.Request) {
}
}

if i == 0 {
response.ErrorResponse(w, http.StatusBadRequest, err, "empty file", h.logger)
return
}

response.SuccessResponse(w, http.StatusOK, "Successfully imported transactions")
}
Loading

0 comments on commit 5614a01

Please sign in to comment.