Skip to content

Commit

Permalink
Merge pull request #12 from ARUNK2121/finalization
Browse files Browse the repository at this point in the history
Finalization
  • Loading branch information
ARUNK2121 authored Oct 21, 2023
2 parents ff2572e + e58d8ba commit 5e35fa6
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 33 deletions.
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mock: ##make mock files using mockgen
mockgen -source=pkg/repository/interface/user.go -destination=pkg/mock/mockrepo/user_mock.go -package=mockrepo
mockgen -source=pkg/usecase/interface/user.go -destination=pkg/mock/mockusecase/user_mock.go -package=mockusecase
mockgen -source=pkg/repository/interface/inventory.go -destination=pkg/mock/mockrepo/inventory_mock.go -package=mockrepo
mockgen -source=pkg/repository/interface/order.go -destination=pkg/mock/mockrepo/order_mock.go -package=mockrepo

swag: ## Generate swagger docs
swag init -g pkg/api/handler/admin.go -o ./cmd/api/docs
Expand Down
27 changes: 23 additions & 4 deletions pkg/api/handler/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ func (i *OrderHandler) CancelOrder(c *gin.Context) {
// @Router /admin/orders/edit/status [put]
func (i *OrderHandler) EditOrderStatus(c *gin.Context) {

status := c.Query("status")
id, err := strconv.Atoi(c.Query("id"))
var status models.EditOrderStatus
err := c.BindJSON(&status)
if err != nil {
errorRes := response.ClientResponse(http.StatusBadRequest, "coonversion to integer not possible", nil, err.Error())
errorRes := response.ClientResponse(http.StatusBadRequest, "conversion to integer not possible", nil, err.Error())
c.JSON(http.StatusBadRequest, errorRes)
return
}
if err := i.orderUseCase.EditOrderStatus(status, id); err != nil {
if err := i.orderUseCase.EditOrderStatus(status.Status, status.OrderID); err != nil {
errorRes := response.ClientResponse(http.StatusBadRequest, "fields provided are in wrong format", nil, err.Error())
c.JSON(http.StatusBadRequest, errorRes)
return
Expand Down Expand Up @@ -185,3 +185,22 @@ func (i *OrderHandler) ReturnOrder(c *gin.Context) {
c.JSON(http.StatusOK, successRes)

}

func (i *OrderHandler) MakePaymentStatusAsPaid(c *gin.Context) {

id, err := strconv.Atoi(c.Query("id"))
if err != nil {
errorRes := response.ClientResponse(http.StatusBadRequest, "conversion to integer not possible", nil, err.Error())
c.JSON(http.StatusBadRequest, errorRes)
return
}
if err := i.orderUseCase.MakePaymentStatusAsPaid(id); err != nil {
errorRes := response.ClientResponse(http.StatusBadRequest, "fields provided are in wrong format", nil, err.Error())
c.JSON(http.StatusBadRequest, errorRes)
return
}

successRes := response.ClientResponse(http.StatusOK, "successfully updated as paid", nil, nil)
c.JSON(http.StatusOK, successRes)

}
4 changes: 2 additions & 2 deletions pkg/domain/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ type AdminOrdersResponse struct {
}

type OrderDetails struct {
Id int `json:"order_id"`
Id int `json:"id" gorm:"id"`
Username string `json:"name"`
Address string `json:"address"`
Paymentmethod string `json:"payment_method"`
Paymentmethod string `json:"payment_method" gorm:"payment_method"`
Total float64 `json:"total"`
}
44 changes: 29 additions & 15 deletions pkg/mock/mockrepo/order_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/repository/interface/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ type OrderRepository interface {
CheckOrder(orderID string, userID int) error
GetOrderDetail(orderID string) (domain.Order, error)

CheckIfTheOrderIsAlreadyReturned(id int) (string, error)
CheckOrderStatusByID(id int) (string, error)
ReturnOrder(id int) error
FindAmountFromOrderID(id int) (float64, error)
CreditToUserWallet(amount float64, walletID int) error
FindUserIdFromOrderID(id int) (int, error)
FindWalletIdFromUserID(userId int) (int, error)
CreateNewWallet(userID int) (int, error)
MakePaymentStatusAsPaid(id int) error
}
20 changes: 15 additions & 5 deletions pkg/repository/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (i *orderRepository) EditOrderStatus(status string, id int) error {
func (or *orderRepository) AdminOrders(status string) ([]domain.OrderDetails, error) {

var orders []domain.OrderDetails
if err := or.DB.Raw("SELECT orders.id AS order_id, users.name AS username, CONCAT(addresses.house_name, ' ', addresses.street, ' ', addresses.city) AS address, payment_methods.payment_name AS payment_method, orders.final_price As total FROM orders JOIN users ON users.id = orders.user_id JOIN payment_methods ON payment_methods.id = orders.payment_method_id JOIN addresses ON orders.address_id = addresses.id WHERE order_status = $1", status).Scan(&orders).Error; err != nil {
if err := or.DB.Raw("SELECT orders.id AS order_id, users.name AS username, CONCAT(addresses.house_name, ' ', addresses.street, ' ', addresses.city, ' ', addresses.state, ' ',) AS address, payment_methods.payment_name AS payment_method, orders.final_price As total FROM orders JOIN users ON users.id = orders.user_id JOIN payment_methods ON payment_methods.id = orders.payment_method_id JOIN addresses ON orders.address_id = addresses.id WHERE order_status = $1", status).Scan(&orders).Error; err != nil {
return []domain.OrderDetails{}, err
}

Expand Down Expand Up @@ -153,7 +153,7 @@ func (i *orderRepository) ReturnOrder(id int) error {

}

func (o *orderRepository) CheckIfTheOrderIsAlreadyReturned(id int) (string, error) {
func (o *orderRepository) CheckOrderStatusByID(id int) (string, error) {

var status string
err := o.DB.Raw("select order_status from orders where id = ?", id).Scan(&status).Error
Expand Down Expand Up @@ -218,15 +218,25 @@ func (o *orderRepository) FindWalletIdFromUserID(userId int) (int, error) {

func (o *orderRepository) CreateNewWallet(userID int) (int, error) {

var wallet_id int
var walletID int
err := o.DB.Exec("Insert into wallets(user_id,amount) values($1,$2)", userID, 0).Error
if err != nil {
return 0, err
}

if err := o.DB.Raw("select id from wallets where user_id=$1", userID).Scan(&wallet_id).Error; err != nil {
if err := o.DB.Raw("select id from wallets where user_id=$1", userID).Scan(&walletID).Error; err != nil {
return 0, err
}

return wallet_id, nil
return walletID, nil
}

func (o *orderRepository) MakePaymentStatusAsPaid(id int) error {

err := o.DB.Exec("UPDATE orders SET payment_status = 'PAID' WHERE id = $1", id).Error
if err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions pkg/routes/adminRoutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func AdminRoutes(engine *gin.RouterGroup, adminHandler *handler.AdminHandler, in
orders := engine.Group("/orders")
{
orders.PUT("/status", orderHandler.EditOrderStatus)
orders.PUT("/payment-status", orderHandler.MakePaymentStatusAsPaid)
orders.GET("", orderHandler.AdminOrders)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/usecase/interface/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type OrderUseCase interface {
EditOrderStatus(status string, id int) error
AdminOrders() (domain.AdminOrdersResponse, error)
ReturnOrder(id int) error
MakePaymentStatusAsPaid(id int) error
}
24 changes: 22 additions & 2 deletions pkg/usecase/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ func (i *orderUseCase) OrderItemsFromCart(userid int, addressid int, paymentid i

func (i *orderUseCase) CancelOrder(id int) error {

err := i.orderRepository.CancelOrder(id)
//the order has to be less than status delivered (pending,shipped) to be canceled
status, err := i.orderRepository.CheckOrderStatusByID(id)
if err != nil {
return err
}

if status != "PENDING" {
return errors.New("order cannot be canceled if you accidently booked kindly return the product")
}

err = i.orderRepository.CancelOrder(id)
if err != nil {
return err
}
Expand Down Expand Up @@ -133,7 +143,7 @@ func (i *orderUseCase) ReturnOrder(id int) error {

//should check if the order is already returned peoples will misuse this security breach
// and will get unlimited money into their wallet
status, err := i.orderRepository.CheckIfTheOrderIsAlreadyReturned(id)
status, err := i.orderRepository.CheckOrderStatusByID(id)
if err != nil {
return err
}
Expand Down Expand Up @@ -185,3 +195,13 @@ func (i *orderUseCase) ReturnOrder(id int) error {
return nil

}

func (i *orderUseCase) MakePaymentStatusAsPaid(id int) error {

err := i.orderRepository.MakePaymentStatusAsPaid(id)
if err != nil {
return err
}
return nil

}
17 changes: 13 additions & 4 deletions pkg/usecase/wishlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,27 @@ func NewWishlistUseCase(repo interfaces.WishlistRepository, offer interfaces.Off
}
}

func (w *wishlistUseCase) AddToWishlist(user_id, inventory_id int) error {
func (w *wishlistUseCase) AddToWishlist(userID, inventoryID int) error {

if err := w.repository.AddToWishlist(user_id, inventory_id); err != nil {
exists, err := w.repository.CheckIfTheItemIsPresentAtWishlist(userID, inventoryID)
if err != nil {
return err
}

if exists {
return errors.New("item already exists in wishlist")
}

if err := w.repository.AddToWishlist(userID, inventoryID); err != nil {
return errors.New("could not add to wishlist")
}

return nil
}

func (w *wishlistUseCase) RemoveFromWishlist(inventory_id int) error {
func (w *wishlistUseCase) RemoveFromWishlist(inventoryID int) error {

if err := w.repository.RemoveFromWishlist(inventory_id); err != nil {
if err := w.repository.RemoveFromWishlist(inventoryID); err != nil {
return errors.New("could not remove from wishlist")
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/utils/models/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ type OrderPaymentDetails struct {
OrderID int `json:"order_id"`
FinalPrice float64 `json:"final_price"`
}

type EditOrderStatus struct {
OrderID int `json:"order_id"`
Status string `json:"order_status"`
}

0 comments on commit 5e35fa6

Please sign in to comment.