-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
111dbba
commit 6cb68a7
Showing
12 changed files
with
187 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package db | ||
|
||
import ( | ||
"reggie/internal/models/model" | ||
) | ||
|
||
type shoppingcartI interface { | ||
List(shoppingCart *model.ShoppingCart) *[]model.ShoppingCart | ||
UpdateNumberById(shoppingCart *model.ShoppingCart) error | ||
Insert(shoppingCart *model.ShoppingCart) *model.ShoppingCart | ||
DeleteByUserId(userId int64) | ||
} | ||
type shoppingcartDao struct { | ||
} | ||
|
||
func (*shoppingcartDao) Insert(shoppingCart *model.ShoppingCart) *model.ShoppingCart { | ||
DBEngine.Select("*").Create(shoppingCart) | ||
return shoppingCart | ||
} | ||
func (*shoppingcartDao) List(shoppingCart *model.ShoppingCart) *[]model.ShoppingCart { | ||
var ( | ||
shop []model.ShoppingCart | ||
//count int64 | ||
) | ||
origin_sql := DBEngine | ||
// 判断是否含有name,有name不为nil,就进行模糊查询。 | ||
if shoppingCart.UserID != 0 { | ||
origin_sql = origin_sql.Where("userId= ?", shoppingCart.UserID) | ||
} | ||
if shoppingCart.SetmealID != 0 { | ||
origin_sql = origin_sql.Where("setmealId =?", shoppingCart.SetmealID) | ||
} | ||
if shoppingCart.DishID != 0 { | ||
origin_sql = origin_sql.Where("dishId=?", shoppingCart.DishID) | ||
} | ||
if shoppingCart.DishFlavor != "" { | ||
origin_sql = origin_sql.Where("dishFlavor =?", shoppingCart.DishFlavor) | ||
} | ||
//origin_sql.Model(&model.Category{}).Count(&count) | ||
origin_sql.Find(&shop) | ||
return &shop | ||
} | ||
|
||
func (*shoppingcartDao) UpdateNumberById(shoppingCart *model.ShoppingCart) error { | ||
DBEngine.Updates(shoppingCart) | ||
return nil | ||
} | ||
func (*shoppingcartDao) DeleteByUserId(userId int64) { | ||
DBEngine.Where("user_id=?", userId).Delete(&model.ShoppingCart{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dto | ||
|
||
import "reggie/internal/models/model" | ||
|
||
type ShoppingCartDTO struct { | ||
DishId int64 `json:"dishId,omitempty"` | ||
SetmealId int64 `json:"setmealId,omitempty"` | ||
DishFlavor string `json:"dishFlavor,omitempty"` | ||
} | ||
|
||
func (s *ShoppingCartDTO) ToShoppingCart() model.ShoppingCart { | ||
var sh model.ShoppingCart | ||
sh.DishID = s.DishId | ||
sh.SetmealID = s.SetmealId | ||
sh.DishFlavor = s.DishFlavor | ||
return sh | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package service | ||
|
||
import ( | ||
"reggie/internal/db" | ||
"reggie/internal/models/model" | ||
"time" | ||
) | ||
|
||
func AddShoppingCart(shoppingCart model.ShoppingCart) { | ||
//判断当前商品是否在购物车中 | ||
shoppingCartList := db.ShopCartDao.List(&shoppingCart) | ||
if shoppingCartList != &[]model.ShoppingCart{} { | ||
shoppingCart := (*shoppingCartList)[0] | ||
shoppingCart.Number += 1 | ||
(*shoppingCartList)[0] = shoppingCart | ||
db.ShopCartDao.UpdateNumberById(&shoppingCart) | ||
} else { | ||
//如果不存在,插入数据,数量就是1 | ||
//判断当前添加到购物车的是菜品还是套餐 | ||
dishId := shoppingCart.DishID | ||
if dishId != 0 { | ||
dish := db.DisDao.GetById(dishId) | ||
shoppingCart.Name = dish.Name | ||
shoppingCart.Image = dish.Image | ||
shoppingCart.Amount = dish.Price | ||
} else { | ||
setmealId := shoppingCart.SetmealID | ||
setmeal := db.MealDao.GetByID(setmealId) | ||
shoppingCart.Name = setmeal.Name | ||
shoppingCart.Image = setmeal.Image | ||
shoppingCart.Amount = setmeal.Price | ||
} | ||
shoppingCart.Number = 1 | ||
shoppingCart.CreateTime = time.Now() | ||
db.ShopCartDao.Insert(&shoppingCart) | ||
} | ||
} | ||
func ShowShoppingCart(id int64) *[]model.ShoppingCart { | ||
shop_cart := model.ShoppingCart{} | ||
shop_cart.UserID = id | ||
return db.ShopCartDao.List(&shop_cart) | ||
} | ||
func CleanShoppingCart(userId int64) { | ||
db.ShopCartDao.DeleteByUserId(userId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/common/hlog" | ||
"net/http" | ||
"reggie/internal/middleware" | ||
"reggie/internal/models/common" | ||
"reggie/internal/models/dto" | ||
"reggie/internal/router/service" | ||
) | ||
|
||
// 添加购物车 | ||
// /user/shoppingCart/add | ||
func AddShoppingCart(ctx context.Context, c *app.RequestContext) { | ||
var shop_dto dto.ShoppingCartDTO | ||
c.Bind(&shop_dto) | ||
shop_cart := shop_dto.ToShoppingCart() | ||
shop_cart.UserID = middleware.GetJwtPayload(c) | ||
hlog.Infof("添加购物车:", shop_cart) | ||
service.AddShoppingCart(shop_cart) | ||
c.JSON(http.StatusOK, common.Result{1, "", nil}) | ||
} | ||
func ListShoppingCart(ctx context.Context, c *app.RequestContext) { | ||
// user | ||
user_id := middleware.GetJwtPayload(c) | ||
c.JSON(http.StatusOK, common.Result{1, "", service.ShowShoppingCart(user_id)}) | ||
} | ||
|
||
// 清空购物车 | ||
func CleanShoppingCart(ctx context.Context, c *app.RequestContext) { | ||
user_id := middleware.GetJwtPayload(c) | ||
// 获取user_id | ||
service.CleanShoppingCart(user_id) | ||
c.JSON(http.StatusOK, common.Result{1, "", nil}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters