|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/entain-test-task/configuration" |
| 10 | + "github.com/entain-test-task/model" |
| 11 | + requestmodel "github.com/entain-test-task/model/request" |
| 12 | + "github.com/entain-test-task/service" |
| 13 | + "github.com/go-openapi/strfmt" |
| 14 | + "github.com/gorilla/mux" |
| 15 | +) |
| 16 | + |
| 17 | +type Transaction struct { |
| 18 | + cfg *configuration.Config |
| 19 | + service *service.Transaction |
| 20 | + validator *model.Validator |
| 21 | +} |
| 22 | + |
| 23 | +func NewTransaction( |
| 24 | + cfg *configuration.Config, |
| 25 | + service *service.Transaction, |
| 26 | +) *Transaction { |
| 27 | + return &Transaction{ |
| 28 | + cfg: cfg, |
| 29 | + service: service, |
| 30 | + validator: model.NewValidator(), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (controller *Transaction) GetAllTransactionsByUserID(w http.ResponseWriter, r *http.Request) { |
| 35 | + params := mux.Vars(r) |
| 36 | + userID := params["user_id"] |
| 37 | + |
| 38 | + if !strfmt.IsUUID4(userID) { |
| 39 | + StatusBadRequest(w, "user_id is not a valid UUID4") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + getAllTransactionsByUserIDResponse, err := controller.service.GetAllTransactionsByUserID(strfmt.UUID4(userID)) |
| 44 | + if err != nil { |
| 45 | + log.Printf("getting all transactions by user ID failed. %v", err) |
| 46 | + StatusInternalServerError(w) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + StatusOK(w, getAllTransactionsByUserIDResponse) |
| 51 | +} |
| 52 | + |
| 53 | +func (controller *Transaction) ProcessRecord(w http.ResponseWriter, r *http.Request) { |
| 54 | + params := mux.Vars(r) |
| 55 | + userID := params["user_id"] |
| 56 | + |
| 57 | + if !strfmt.IsUUID4(userID) { |
| 58 | + StatusBadRequest(w, "user_id is not a valid UUID4") |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + var processRecordRequest requestmodel.ProcessRecordRequest |
| 63 | + if err := json.NewDecoder(r.Body).Decode(&processRecordRequest); err != nil { |
| 64 | + handleDecodeError(err, w) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + validationErrors := controller.validator.ValidateRequest(processRecordRequest) |
| 69 | + if validationErrors != nil { |
| 70 | + log.Printf("validating request failed. %v", validationErrors) |
| 71 | + StatusBadRequestWithErrors(w, "validation error", validationErrors) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + processRecordResponse, err := controller.service.ProcessRecord(strfmt.UUID4(userID), processRecordRequest) |
| 76 | + if err != nil { |
| 77 | + handleProcessRecordError(w, err) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + StatusOK(w, processRecordResponse) |
| 82 | +} |
| 83 | + |
| 84 | +func (controller *Transaction) CancelLatestOddTransactionRecords() { |
| 85 | + controller.service.CancelLatestOddTransactionRecords(controller.cfg.NumberOfLatestRecordsForCancelling) |
| 86 | +} |
| 87 | + |
| 88 | +func handleProcessRecordError(w http.ResponseWriter, err error) { |
| 89 | + switch err.Error() { |
| 90 | + case model.ErrUserNotFound().Error(): |
| 91 | + StatusUnprocessableEntity(w, model.ErrUserNotFound().Error()) |
| 92 | + case model.ErrInsufficientBalance().Error(): |
| 93 | + StatusUnprocessableEntity(w, model.ErrInsufficientBalance().Error()) |
| 94 | + case model.ErrTransactionAlreadyExists().Error(): |
| 95 | + StatusUnprocessableEntity(w, model.ErrTransactionAlreadyExists().Error()) |
| 96 | + default: |
| 97 | + log.Printf("%s. %v", "processing record failed", err) |
| 98 | + StatusInternalServerError(w) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func handleDecodeError(err error, w http.ResponseWriter) { |
| 103 | + if err == nil { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + handleUnmarshalTypeError(err, w) |
| 108 | + |
| 109 | + log.Printf("decoding request failed. %v", err) |
| 110 | + StatusInternalServerError(w) |
| 111 | +} |
| 112 | + |
| 113 | +func handleUnmarshalTypeError(err error, w http.ResponseWriter) { |
| 114 | + typeErr, ok := err.(*json.UnmarshalTypeError) |
| 115 | + if !ok { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + StatusBadRequestWithErrors(w, "validation error", []error{ |
| 120 | + errors.New(typeErr.Field + " is not within allowed range"), |
| 121 | + }) |
| 122 | +} |
0 commit comments