Skip to content
Snippets Groups Projects
Commit 24d33d08 authored by Jerry Xu's avatar Jerry Xu
Browse files

cart and favourite apis

parent a94c352c
Branches jerry
No related tags found
No related merge requests found
......@@ -3,37 +3,27 @@ package com.example.freshonline.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.freshonline.service.CartService;
import com.example.freshonline.service.StockedGoodsService;
import com.example.freshonline.utils.PicUtils;
import com.example.freshonline.utils.RespBuilder;
import com.example.freshonline.utils.ValidationChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.example.freshonline.model.Cart;
import com.example.freshonline.model.joined_tables.GoodsCategory;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author Josh Sun
*/
@RestController
public class CartController {
@Autowired
private CartService cartService;
static public class ID {
static public class Id {
private int userId;
private int goodsId;
public ID() {
public Id() {
}
public int getUserId() {
......@@ -53,13 +43,39 @@ public class CartController {
}
}
@GetMapping("/cart_element")
public JSONObject getCartGood(@RequestParam("user_id") String user_id, @RequestParam("goods_id") String goods_id) {
JSONObject res = new JSONObject();
try {
Integer userId = Integer.parseInt(user_id);
Integer goodsId = Integer.parseInt(goods_id);
Cart cart_goods = cartService.getCartEntry(userId,goodsId);
JSONObject data = (JSONObject) JSONObject.toJSON(cart_goods);
res.put("code", 0);
res.put("data", data);
return res;
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
res.put("code", 1);
res.put("msg", sw.toString());
return res;
}
}
@GetMapping("/cart")
public JSONObject getGoodsDetails(@RequestBody JSONObject req) {
public JSONObject getCartGoods(@RequestParam("user_id") String user_id) {
JSONObject res = new JSONObject();
try {
ID id = req.toJavaObject(ID.class);
List<Cart> cart_goods = cartService.getCart(id.getUserId());
Integer id = Integer.parseInt(user_id);
List<Cart> cart_goods = cartService.getCart(id);
JSONArray data = (JSONArray) JSONArray.toJSON(cart_goods);
res.put("code", 0);
res.put("data", data);
......@@ -123,7 +139,7 @@ public class CartController {
JSONObject res = new JSONObject();
try {
ID id = req.toJavaObject(ID.class);
Id id = req.toJavaObject(Id.class);
cartService.deleteFromCart(id.getUserId(),id.getGoodsId());
res.put("code", 0);
return res;
......
package com.example.freshonline.controller;
import com.example.freshonline.service.FavouriteService;
import com.example.freshonline.model.Favorite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@RestController
public class FavouriteController {
@Autowired
private FavouriteService favouriteService;
@GetMapping("/favourite")
public JSONObject checkFavourite(@RequestParam("user_id") String user_id, @RequestParam("goods_id") String goods_id) {
JSONObject res = new JSONObject();
try {
Integer userID = Integer.parseInt(user_id);
Integer goodsID = Integer.parseInt(goods_id);
res.put("code", 0);
res.put("result", favouriteService.isFavourite(userID, goodsID));
return res;
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
res.put("code", 1);
res.put("msg", sw.toString());
return res;
}
}
@PutMapping("/favourite")
public JSONObject addFavEntry(@RequestBody JSONObject req) {
JSONObject res = new JSONObject();
try {
Favorite favorite = req.toJavaObject(Favorite.class);
favouriteService.addFavourite(favorite);
res.put("code", 0);
return res;
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
res.put("code", 1);
res.put("msg", sw.toString());
return res;
}
}
@DeleteMapping("/favourite")
public JSONObject deleteFavEntry(@RequestBody JSONObject req) {
JSONObject res = new JSONObject();
try {
Favorite favorite = req.toJavaObject(Favorite.class);
favouriteService.deleteFavourite(favorite);
res.put("code", 0);
return res;
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
res.put("code", 1);
res.put("msg", sw.toString());
return res;
}
}
}
......@@ -36,6 +36,10 @@ public class CartService {
return cartMapper.selectByUserID(userId);
}
public Cart getCartEntry(Integer userId, Integer goodsId) {
return cartMapper.selectByPrimaryKey(userId, goodsId);
}
......
package com.example.freshonline.service;
import com.example.freshonline.dao.FavoriteMapper;
import com.example.freshonline.model.Favorite;
import com.example.freshonline.model.FavoriteExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class FavouriteService {
@Autowired
private FavoriteMapper favoriteMapper;
public boolean isFavourite(Integer userID, Integer goodsID){
FavoriteExample example = new FavoriteExample();
example.or().andUserIdEqualTo(userID).andGoodsIdEqualTo(goodsID);
long count = favoriteMapper.countByExample(example);
return count!=0;
}
public void addFavourite(Favorite favorite) {
favoriteMapper.insert( favorite);
}
public void deleteFavourite(Favorite favorite) {
favoriteMapper.deleteByPrimaryKey( favorite.getUserId(), favorite.getGoodsId());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment