fixed linter errors
This commit is contained in:
parent
d38a61307d
commit
1ae02b3bc6
15 changed files with 242 additions and 117 deletions
11
server/errors.go
Normal file
11
server/errors.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// errors are in a dedicated file, because they are used in more than one place
|
||||
package server
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrInvalidForm = errors.New("the form has invalid syntax")
|
||||
var ErrSavingToDatabase = errors.New("error saving to the database")
|
||||
var ErrWrongValue = errors.New("error converting or getting value")
|
||||
|
||||
// should not occur during runtime
|
||||
var ErrCatalogueNotFound = errors.New("cake catalogue not found on the server")
|
||||
|
|
@ -2,19 +2,19 @@ package server
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Bronku/iroon/models"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func monthInterval(y int, m time.Month) (firstDay, lastDay time.Time) {
|
||||
firstDay = time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)
|
||||
lastDay = time.Date(y, m+1, 1, 0, 0, 0, -1, time.UTC)
|
||||
func monthInterval(y int, m time.Month) (time.Time, time.Time) {
|
||||
firstDay := time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)
|
||||
lastDay := time.Date(y, m+1, 1, 0, 0, 0, -1, time.UTC)
|
||||
return firstDay, lastDay
|
||||
}
|
||||
|
||||
|
|
@ -25,11 +25,13 @@ func (h *Server) orders(_ *http.Request) (any, int, error) {
|
|||
)
|
||||
y, m, _ = time.Now().Date()
|
||||
first, last := monthInterval(y, m)
|
||||
fmt.Println(first, last)
|
||||
var orders []models.Order
|
||||
result := h.db.Preload("OrderItems.Product").Preload(clause.Associations).Where("date between ? and ?", first, last).Find(&orders)
|
||||
result := h.db.
|
||||
Preload("OrderItems.Product").
|
||||
Preload(clause.Associations).
|
||||
Where("date between ? and ?", first, last).
|
||||
Find(&orders)
|
||||
if result.Error != nil {
|
||||
log.Println("eroor getting orders")
|
||||
return nil, http.StatusInternalServerError, result.Error
|
||||
}
|
||||
data := struct {
|
||||
|
|
@ -41,16 +43,20 @@ func (h *Server) orders(_ *http.Request) (any, int, error) {
|
|||
}
|
||||
|
||||
func (h *Server) ordersSearch(r *http.Request) (any, int, error) {
|
||||
from, err := time.Parse("2006-01-02", r.URL.Query().Get("from"))
|
||||
startTime, err := time.Parse("2006-01-02", r.URL.Query().Get("from"))
|
||||
if err != nil {
|
||||
from = time.Time{}
|
||||
startTime = time.Time{}
|
||||
}
|
||||
to, err := time.Parse("2006-01-02", r.URL.Query().Get("to"))
|
||||
endTime, err := time.Parse("2006-01-02", r.URL.Query().Get("to"))
|
||||
if err != nil {
|
||||
to = time.Time{}
|
||||
endTime = time.Time{}
|
||||
}
|
||||
var orders []models.Order
|
||||
result := h.db.Preload("OrderItems.Product").Preload(clause.Associations).Where("date between ? and ?", from, to).Find(&orders)
|
||||
result := h.db.
|
||||
Preload("OrderItems.Product").
|
||||
Preload(clause.Associations).
|
||||
Where("date between ? and ?", startTime, endTime).
|
||||
Find(&orders)
|
||||
return orders, http.StatusOK, result.Error
|
||||
}
|
||||
|
||||
|
|
@ -66,14 +72,17 @@ func (h *Server) cake(r *http.Request) (any, int, error) {
|
|||
return models.Product{}, http.StatusOK, nil
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(url[2])
|
||||
cakeID, err := strconv.Atoi(url[2])
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return nil, http.StatusBadRequest, fmt.Errorf("%w cakeID", ErrWrongValue)
|
||||
}
|
||||
|
||||
var cake models.Product
|
||||
result := h.db.First(&cake, id)
|
||||
return cake, http.StatusOK, result.Error
|
||||
result := h.db.First(&cake, cakeID)
|
||||
if result.Error != nil {
|
||||
return models.Product{}, http.StatusNotFound, gorm.ErrRecordNotFound
|
||||
}
|
||||
return cake, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (h *Server) order(r *http.Request) (any, int, error) {
|
||||
|
|
@ -85,7 +94,7 @@ func (h *Server) order(r *http.Request) (any, int, error) {
|
|||
|
||||
result := h.db.Find(&data.Catalogue)
|
||||
if result.Error != nil {
|
||||
return nil, http.StatusInternalServerError, result.Error
|
||||
return nil, http.StatusInternalServerError, ErrCatalogueNotFound
|
||||
}
|
||||
|
||||
url := strings.Split(r.URL.String(), "/")
|
||||
|
|
@ -93,15 +102,14 @@ func (h *Server) order(r *http.Request) (any, int, error) {
|
|||
return data, http.StatusOK, nil
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(url[2])
|
||||
orderID, err := strconv.Atoi(url[2])
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return nil, http.StatusBadRequest, fmt.Errorf("%w orderID", ErrWrongValue)
|
||||
}
|
||||
|
||||
result = h.db.Preload("OrderItems.Product").Preload(clause.Associations).Find(&data.Order, id)
|
||||
//result = h.db.Find(&data.Order, id)
|
||||
result = h.db.Preload("OrderItems.Product").Preload(clause.Associations).Find(&data.Order, orderID)
|
||||
if result.Error != nil {
|
||||
return nil, http.StatusNotFound, result.Error
|
||||
return nil, http.StatusNotFound, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
return data, http.StatusOK, nil
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Bronku/iroon/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func atoui(input string) (uint, error) {
|
||||
|
|
@ -19,73 +20,87 @@ func atoui(input string) (uint, error) {
|
|||
func (h *Server) postCake(r *http.Request) (any, int, error) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError, err
|
||||
return nil, http.StatusBadRequest, ErrInvalidForm
|
||||
}
|
||||
|
||||
var n models.Product
|
||||
n.ID, err = atoui(r.FormValue("id"))
|
||||
var postedCake models.Product
|
||||
postedCake.ID, err = atoui(r.FormValue("id"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return nil, http.StatusBadRequest, fmt.Errorf("%w cakeID", ErrWrongValue)
|
||||
}
|
||||
n.Name = r.FormValue("name")
|
||||
n.Price, err = atoui(r.FormValue("price"))
|
||||
postedCake.Name = r.FormValue("name")
|
||||
postedCake.Price, err = atoui(r.FormValue("price"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return nil, http.StatusBadRequest, fmt.Errorf("%w price", ErrWrongValue)
|
||||
}
|
||||
result := h.db.Save(&n)
|
||||
return n, http.StatusAccepted, result.Error
|
||||
result := h.db.Save(&postedCake)
|
||||
return postedCake, http.StatusAccepted, errors.Join(ErrSavingToDatabase, result.Error)
|
||||
}
|
||||
|
||||
func (h *Server) postOrder(r *http.Request) (any, int, error) {
|
||||
func (h *Server) parseOrder(r *http.Request) (models.Order, error) {
|
||||
var cakes []models.Product
|
||||
result := h.db.Find(&cakes)
|
||||
if result.Error != nil {
|
||||
return nil, http.StatusInternalServerError, result.Error
|
||||
return models.Order{}, ErrCatalogueNotFound
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
log.Println("received form:", r.PostForm)
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return models.Order{}, ErrInvalidForm
|
||||
}
|
||||
|
||||
var n models.Order
|
||||
n.ID, err = atoui(r.FormValue("id"))
|
||||
var postedOrder models.Order
|
||||
postedOrder.ID, err = atoui(r.FormValue("id"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return models.Order{}, fmt.Errorf("%w orderID", ErrWrongValue)
|
||||
}
|
||||
n.Prepaid, err = atoui(r.FormValue("paid"))
|
||||
postedOrder.Prepaid, err = atoui(r.FormValue("paid"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return models.Order{}, fmt.Errorf("%w prepaid", ErrWrongValue)
|
||||
}
|
||||
n.Date, err = time.Parse("2006-01-02", r.FormValue("date"))
|
||||
postedOrder.Date, err = time.Parse("2006-01-02", r.FormValue("date"))
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
return models.Order{}, fmt.Errorf("%w date", ErrWrongValue)
|
||||
}
|
||||
|
||||
n.OrderItems = make([]models.OrderItem, 0)
|
||||
postedOrder.OrderItems = make([]models.OrderItem, 0)
|
||||
for _, e := range cakes {
|
||||
count, err := atoui(r.FormValue(fmt.Sprintf("cake[%d]", e.ID)))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
n.OrderItems = append(n.OrderItems, models.OrderItem{Amount: count, Product: e})
|
||||
postedOrder.OrderItems = append(postedOrder.OrderItems, models.OrderItem{Amount: count, Product: e})
|
||||
}
|
||||
|
||||
n.Name = strings.TrimSpace(r.FormValue("name"))
|
||||
n.Surname = strings.TrimSpace(r.FormValue("surname"))
|
||||
n.Phone = strings.TrimSpace(r.FormValue("phone"))
|
||||
n.Location = strings.TrimSpace(r.FormValue("location"))
|
||||
n.Status = strings.TrimSpace(r.FormValue("status"))
|
||||
|
||||
log.Println("parsed order:", n)
|
||||
err = h.db.Save(&n).Error
|
||||
if err != nil {
|
||||
log.Println("save: ", err)
|
||||
}
|
||||
err = h.db.Model(&n).Association("OrderItems").Replace(n.OrderItems)
|
||||
if err != nil {
|
||||
log.Println("replace: ", err)
|
||||
}
|
||||
return n, http.StatusAccepted, nil
|
||||
postedOrder.Name = strings.TrimSpace(r.FormValue("name"))
|
||||
postedOrder.Surname = strings.TrimSpace(r.FormValue("surname"))
|
||||
postedOrder.Phone = strings.TrimSpace(r.FormValue("phone"))
|
||||
postedOrder.Location = strings.TrimSpace(r.FormValue("location"))
|
||||
postedOrder.Status = strings.TrimSpace(r.FormValue("status"))
|
||||
return postedOrder, nil
|
||||
}
|
||||
|
||||
func (h *Server) postOrder(r *http.Request) (any, int, error) {
|
||||
postedOrder, err := h.parseOrder(r)
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
// i have no idea why this works, and other methods don't
|
||||
err = h.db.Transaction(func(tx *gorm.DB) error {
|
||||
err = tx.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&postedOrder).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("didn't save the order, %w", err)
|
||||
}
|
||||
err = tx.Model(&postedOrder).Association("OrderItems").Replace(postedOrder.OrderItems)
|
||||
if err != nil {
|
||||
return fmt.Errorf("didn't save the order items %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError, errors.Join(ErrSavingToDatabase, err)
|
||||
}
|
||||
|
||||
return postedOrder, http.StatusAccepted, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue