this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 53 lines 1.4 kB view raw
1package handler 2 3import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/Tulkdan/payment-gateway/internal/dto" 8 "github.com/Tulkdan/payment-gateway/internal/service" 9 "go.uber.org/zap" 10) 11 12type PaymentsHandler struct { 13 paymentService *service.PaymentService 14 15 logger *zap.Logger 16} 17 18func NewPaymentsHandler(paymentsService *service.PaymentService, logger *zap.Logger) *PaymentsHandler { 19 return &PaymentsHandler{ 20 paymentService: paymentsService, 21 logger: logger.Named("PaymentHandler"), 22 } 23} 24 25func (p *PaymentsHandler) Create(w http.ResponseWriter, r *http.Request) { 26 var body dto.PaymentInput 27 if err := json.NewDecoder(r.Body).Decode(&body); err != nil { 28 p.logger.Error("Body incomplete", 29 zap.String("error", err.Error()), 30 zap.String("requestId", r.Context().Value("request-id").(string))) 31 32 http.Error(w, err.Error(), http.StatusBadRequest) 33 return 34 } 35 36 response, err := p.paymentService.CreatePayment(r.Context(), body) 37 if err != nil { 38 p.logger.Error("Failed to create payment", 39 zap.String("error", err.Error()), 40 zap.String("requestId", r.Context().Value("request-id").(string))) 41 42 http.Error(w, err.Error(), http.StatusBadRequest) 43 return 44 } 45 46 p.logger.Debug("Processed request", 47 zap.Any("response", response), 48 zap.String("requestId", r.Context().Value("request-id").(string))) 49 50 w.Header().Set("Content-Type", "application/json") 51 w.WriteHeader(http.StatusOK) 52 json.NewEncoder(w).Encode(response) 53}