Documentation ¶
Index ¶
- Variables
- type Authorizer
- type Cart
- type CartItem
- type CreateCartResponse
- type DB
- type Discount
- type DiscountType
- type ErrorResponse
- type GetCartByIDResponse
- type GetCartResponse
- type NotAuthorizedError
- type Operation
- type OperationType
- type Price
- type Request
- type Response
- type Service
- func (svc *Service) AdminRouter(base string) *chi.Mux
- func (svc *Service) Create(w http.ResponseWriter, req *http.Request)
- func (svc *Service) CreateWithoutSession(w http.ResponseWriter, req *http.Request)
- func (svc *Service) Delete(w http.ResponseWriter, req *http.Request)
- func (svc *Service) DeleteWithID(w http.ResponseWriter, req *http.Request)
- func (svc *Service) Get(w http.ResponseWriter, req *http.Request)
- func (svc *Service) GetWithID(w http.ResponseWriter, req *http.Request)
- func (svc *Service) Router(base string) *chi.Mux
- func (svc *Service) Update(w http.ResponseWriter, req *http.Request)
- func (svc *Service) UpdateWithID(w http.ResponseWriter, req *http.Request)
- type UpdateCartRequest
- type UpdateCartResponse
- type UserContext
- type UserContextFetcher
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Authorizer ¶
type CreateCartResponse ¶
type DB ¶
type DB interface { // CreateCartForSession will instantiate a brand new empty Cart for the session. // // If no matching session is found it will return ErrSessionNotFound. // If a Cart already exists for that session, it will return ErrAlreadyExists. CreateCartForSession(sessionToken string) (Cart, error) // CreateCart will create a Cart without assigning it to a session. CreateCart() (Cart, error) // DeleteCart will delete the Cart matching the ID. It doesn't check // for permissions and should only be called after user has been authorized. // // If no Cart could be found, it will return ErrCartNotFound. DeleteCart(cartID string) error // UpdateCart will update the cart matching the cart.ID field. It doesn't check // for permissions and should only be called after user has been authorized. // // If no Cart could be found, it will return ErrCartNotFound. UpdateCart(cart Cart) error // LookupCart will find the Cart matching the ID. It doesn't check // for permissions and should only be called after user has been authorized. // // If no cart could be found, it will return ErrCartNotFound. LookupCart(cartID string) (Cart, error) // LookupCart will find the Cart for this session. // // If no matching session is found, it will return ErrSessionNotFound. // If no cart could be found, it will return ErrCartNotFound. LookupCartForSession(sessionToken string) (Cart, error) // AssignCartToSession will assign the cart specified by ID to the given // session. // // If no matching session is found, it will return ErrSessionNotFound. // If no cart could be found, it will return ErrCartNotFound. AssignCartToSession(cartID, sessionToken string) error }
type Discount ¶
type Discount struct { ID string `json:"id"` Type DiscountType `json:"type"` Value float64 `json:"value"` }
type DiscountType ¶
type DiscountType string
const ( PercentageDiscount DiscountType = "percentage" FixedAmountDiscount DiscountType = "fixed-amount" )
type ErrorResponse ¶
type GetCartByIDResponse ¶
type GetCartResponse ¶
type NotAuthorizedError ¶
func (NotAuthorizedError) Error ¶
func (e NotAuthorizedError) Error() string
type Operation ¶
type Operation struct { Resource string `json:"resource"` Type OperationType `json:"type"` }
type OperationType ¶
type OperationType string
const ( CreateOp OperationType = "create" ReadOp OperationType = "read" UpdateOp OperationType = "update" DeleteOp OperationType = "delete" )
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
func NewService ¶
func NewService(db DB, usrCtxFetcher UserContextFetcher, authorizer Authorizer, logger *slog.Logger) (*Service, error)
func (*Service) AdminRouter ¶
func (*Service) Create ¶
func (svc *Service) Create(w http.ResponseWriter, req *http.Request)
Create will create a new Cart for the current session.
Status codes:
- 201: Created successfully
- 400: No session found for request
- 409: Cart already exists
- 500: unexpected error
func (*Service) CreateWithoutSession ¶
func (svc *Service) CreateWithoutSession(w http.ResponseWriter, req *http.Request)
CreateWithoutSession will create an empty Cart without assigning it to a session.
Errors:
- NotAuthorizedError if user is not authorized
- ErrCartNotFound if cart is not found
Status Codes:
- 200: OK
- 403: Forbidden
- 404: Cart not found
func (*Service) Delete ¶
func (svc *Service) Delete(w http.ResponseWriter, req *http.Request)
Delete will delete the Cart for the current session. It will reject the Cart if the ID suplied does not match the expected one.
Status codes:
- 204: Deleted successfully
- 400: No session found for request
- 404: No cart found for this session
- 500: unexpected error
func (*Service) DeleteWithID ¶
func (svc *Service) DeleteWithID(w http.ResponseWriter, req *http.Request)
Delete will delete the Cart with the supploed ID.
Status codes:
- 204: Deleted successfully
- 400: No session found for request
- 404: No cart found
- 500: unexpected error
func (*Service) Get ¶
func (svc *Service) Get(w http.ResponseWriter, req *http.Request)
Get will return the Cart associated to the current user's session.
Status codes:
- 200: OK
- 400: No session found for request
- 404: No cart found for session
- 500: unexpected error
func (*Service) GetWithID ¶
func (svc *Service) GetWithID(w http.ResponseWriter, req *http.Request)
GetWithID will return the Cart if found.
Errors:
- NotAuthorizedError if user is not authorized
- ErrCartNotFound if cart is not found
Status Codes:
- 200: OK
- 403: Forbidden
- 404: Cart not found
func (*Service) Update ¶
func (svc *Service) Update(w http.ResponseWriter, req *http.Request)
Update will update the Cart for the current session. It will reject the Cart if the ID suplied does not match.
Status codes:
- 200: Updated successfully
- 400: No session found for request
- 403: Cart ID is not the ID matching this session's Cart
- 404: No cart found for this session
- 500: unexpected error
func (*Service) UpdateWithID ¶
func (svc *Service) UpdateWithID(w http.ResponseWriter, req *http.Request)
Update will update the Cart. It will override the Cart ID to ensure no accidental changes.
Status codes:
- 200: Updated successfully
- 400: No session found for request
- 404: No cart found
- 500: unexpected error
type UpdateCartRequest ¶
type UpdateCartResponse ¶
type UserContext ¶
func (UserContext) IsLoggedIn ¶
func (u UserContext) IsLoggedIn() bool
type UserContextFetcher ¶
type UserContextFetcher interface {
GetUserContext(req *http.Request) (UserContext, error)
}
UserContextFetcher encapsulates functionality for fetching session tokens and user IDs from a request. Returns ErrSessionNotFound if no session could be found.