山东大学项目实训-地图圈系统-后端开发(10)——“收藏”接口开发

“收藏”接口开发

用户需要关于“我的收藏”方面的功能,设计到数据库表的增删改查,比如添加收藏,删除收藏,获取某个用户的所有收藏等。由于开发的接口比较多,下面粘贴部分有代表性的接口的相关代码:

1、检测某个用户下某个收藏是否已经存在(1)

    @GetMapping("/ifexist/{name}")
    public Object ifexist(@PathVariable("name") String name, HttpServletRequest request) {
        String token;
        token = request.getHeader("token");// 从 http 请求头中取出 token
        System.out.println(token);
        String username;
        username = JWT.decode(token).getAudience().get(0);
        System.out.println(username);
        if (username != null) {
            User user;
            user = userRepository.findById(username).orElse(null);
            if (user != null) {
                List<Collect> list1 = collectRepository.findAll();
                List<Collect> list2 = new ArrayList<>();
                for (Collect collect : list1) {
                    if (collect.getUsername().equals(username)) {
                        list2.add(collect);
                    }
                }
                for (Collect collect : list2) {
                    if (collect.getName().equals(name)) {
                        JSONObject object = new JSONObject();
                        object.put("ifexist", true);
                        object.put("status", 200);
                        return object;
                    }
                }
                JSONObject object = new JSONObject();
                object.put("ifexist", false);
                object.put("status", 200);
                return object;
            }
            JSONObject object = new JSONObject();
            object.put("info", "该用户不存在");
            object.put("status", 401);
            return object;
        }
        JSONObject object = new JSONObject();
        object.put("info", "获取不到用户名");
        object.put("status", 402);
        return object;
    }

需要给出一些说明,接口的接收参数中并不包含“用户名”,这是因为这个接口是给网站使用的,网站在向后端请求数据时会在请求头里包含一个token,token里包含了用户的信息,通过解析token即可知道发送请求的用户是谁。

2、检测某个用户下某个收藏是否已经存在(2)

    @GetMapping("/ifexist/{username}/{name}")
    public Object ifexist(@PathVariable("username") String username, @PathVariable("name") String name) {
        List<Collect> list;
        list = collectRepository.findAll();
        for (Collect collect : list) {
            if (collect.getUsername().equals(username) && collect.getName().equals(name)) {
                JSONObject object = new JSONObject();
                object.put("collect", collect);
                object.put("ifexist", true);
                object.put("status", 200);
                return object;
            }
        }
        JSONObject object = new JSONObject();
        object.put("ifexist", false);
        object.put("status", 200);
        return object;
    }

这个接口和上个接口的作用一样,检测某个用户是否已经添加某个收藏,与上一个接口不同的是,该接口为了给微信小程序和APP调用,并不是网站,因为微信小程序和APP不能包含请求头,因此无法通过请求头中的token获取到调用接口的用户,只能加一个参数“username”用来确定是哪个用户调用了接口。

3、添加收藏

    @PostMapping("/addcollect")
    public Object addcollect(@RequestBody Collect collect) {
        List<Collect> list1;
        list1 = collectRepository.findAll();
        int count1;
        count1 = list1.size();
        Collect collect1 = new Collect();
        collect1.setUsername(collect.getUsername());
        collect1.setLatitude(collect.getLatitude());
        collect1.setLongitude(collect.getLongitude());
        collect1.setAddress(collect.getAddress());
        collect1.setName(collect.getName());
        Collect collect2;
        collect2 = collectRepository.save(collect1);
        List<Collect> list2;
        list2 = collectRepository.findAll();
        int count2;
        count2 = list2.size();
        if ((collect2.getUsername() != null) && (count1 + 1 == count2)) {
            JSONObject object = new JSONObject();
            object.put("info", "收藏成功");
            object.put("collect", collect2);
            object.put("status", 200);
            return object;
        } else {
            JSONObject object = new JSONObject();
            object.put("info", "收藏失败");
            object.put("status", 401);
            return object;
        }
    }

这个接口用来向数据库添加一条记录,并发该记录返还给用户。

4、获取所有记录

    @GetMapping("/findall")
    public Object findall() {
        List<Collect> list;
        list = collectRepository.findAll();
        int count;
        count = list.size();
        if (count > 0) {
            JSONObject object = new JSONObject();
            object.put("collectList", list);
            object.put("status", 200);
            return object;
        } else {
            JSONObject object = new JSONObject();
            object.put("info", "查询失败");
            object.put("status", 401);
            return object;
        }
    }

这个接口为了给网站调用,网站的管理端需要看到所有用户的收藏信息,因子要获取所有人的收藏记录。

5、获取某个用户的所有收藏

    @GetMapping("/findall/{username}")
    public Object findall(@PathVariable("username") String username) {
        List<Collect> list1;
        list1 = collectRepository.findAll();
        List<Collect> list2 = new ArrayList<>();
        for (Collect collect : list1) {
            if (collect.getUsername().equals(username)) {
                list2.add(collect);
            }
        }
        int count = list2.size();
        if (count > 0) {
            JSONObject object = new JSONObject();
            object.put("collectList", list2);
            object.put("status", 200);
            return object;
        } else {
            JSONObject object = new JSONObject();
            object.put("info", "查询结果为空");
            object.put("status", 401);
            return object;
        }
    }

这个接口用来返回某个用户所有的收藏信息。

6、根据id查询“收藏”

    @GetMapping("/findbyid/{id}")
    public Object findbyid(@PathVariable("id") Integer id) {
        Collect collect;
        collect = collectRepository.findById(id).orElse(null);
        if (collect != null) {
            JSONObject object = new JSONObject();
            object.put("collect", collect);
            object.put("status", 200);
            return object;
        } else {
            JSONObject object = new JSONObject();
            object.put("info", "查询结果为空");
            object.put("status", 401);
            return object;
        }
    }

7、根据username和name删除

    @DeleteMapping("/deletebyname/{username}/{name}")
    public Object deletebyname(@PathVariable("username") String username, @PathVariable("name") String name) {
        if (username != null) {
            List<Collect> list;
            list = collectRepository.findAll();
            for (Collect collect : list) {
                if (collect.getUsername().equals(username) && collect.getName().equals(name)) {
                    Integer id = collect.getId();
                    collectRepository.deleteById(id);
                    JSONObject object = new JSONObject();
                    object.put("info", "删除成功");
                    object.put("status", 200);
                    return object;
                }
            }
            JSONObject object = new JSONObject();
            object.put("info", "删除失败");
            object.put("status", 401);
            return object;
        } else {
            JSONObject object = new JSONObject();
            object.put("info", "未获取到用户名");
            object.put("status", 402);
            return object;
        }
    }

8、根据id删除

    @DeleteMapping("/deletebyid/{id}")
    public Object deletebyid(@PathVariable("id") Integer id) {
        List<Collect> list1;
        int count1;
        Collect collect1;
        collect1 = collectRepository.findById(id).orElse(null);
        if (collect1 == null) {
            JSONObject object = new JSONObject();
            object.put("info", "不存在");
            object.put("status", 401);
            return object;
        }
        list1 = collectRepository.findAll();
        count1 = list1.size();
        collectRepository.deleteById(id);
        List<Collect> list2;
        int count2;
        list2 = collectRepository.findAll();
        count2 = list2.size();
        Collect collect2;
        collect2 = collectRepository.findById(id).orElse(null);
        if ((count1 - 1 == count2) && (collect2 == null)) {
            JSONObject object = new JSONObject();
            object.put("info", "删除成功");
            object.put("status", 200);
            return object;
        } else {
            JSONObject object = new JSONObject();
            object.put("info", "删除失败");
            object.put("status", 402);
            return object;
        }
    }

9、查询某个用户的收藏数

    @GetMapping("/count/{username}")
    public Object count(@PathVariable("username") String username) {
        List<Collect> list1;
        int count;
        list1 = collectRepository.findAll();
        List<Collect> list2 = new ArrayList<>();
        for (Collect collect : list1) {
            if (collect.getUsername().equals(username)) {
                list2.add(collect);
            }
        }
        count = list2.size();
        JSONObject object = new JSONObject();
        object.put("count", count);
        object.put("status", 200);
        return object;
    }

10、返回所有记录数

    @GetMapping("/count")
    public Object count() {
        List<Collect> list;
        int count;
        list = collectRepository.findAll();
        count = list.size();
        JSONObject object = new JSONObject();
        object.put("count", count);
        object.put("status", 200);
        return object;
    }

11、返回每个用户的记录数,并排序

    @GetMapping("/usercount")
    public Object usercount() {
        Map<String, Integer> collectMap = new TreeMap<String, Integer>();
        List<User> userList = userRepository.findAll();
        for (User user : userList) {
            collectMap.put(user.getUsername(), 0);
        }
        List<Collect> collectList = collectRepository.findAll();
        Integer i;
        for (Collect collect : collectList) {
            i = (Integer) collectMap.get(collect.getUsername());
            collectMap.put(collect.getUsername(), i + 1);
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(collectMap.entrySet());
        list.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        for (Map.Entry<String, Integer> mapping : list) {
            System.out.println(mapping.getKey() + ":" + mapping.getValue());
        }
        JSONObject object = new JSONObject();
        object.put("collectCount", list);
        object.put("status", 200);
        return object;
    }

12、分页返回收藏记录

    @GetMapping("/findall/{page}/{size}")
    public Object findall(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        // 分页查询所有用户,第一个参数是一页的user个数,第二个是页数
        Page<Collect> list;
        PageRequest pageRequest;
        pageRequest = PageRequest.of(page, size);
        list = collectRepository.findAll(pageRequest);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor("yyyy-MM-dd"));
        JSONArray collectPageList = JSONArray.fromObject(list, jsonConfig);
        JSONObject object = new JSONObject();
        object.put("collectPageList", collectPageList);
        object.put("status", 200);
        return object;
    }

13、返回某个名称的收藏

    @GetMapping("/findallByName/{name}")
    public Object findallByName(@PathVariable("name") String name) {
        List<Collect> list1 = collectRepository.findAll();
        List<Collect> list2 = new ArrayList<>();
        for (Collect collect : list1) {
            if (collect.getName().equals(name)) {
                list2.add(collect);
            }
        }
        int count = list2.size();
        if (count != 0) {
            JSONObject object = new JSONObject();
            object.put("collectList", list2);
            object.put("status", 200);
            return object;
        } else {
            JSONObject object = new JSONObject();
            object.put("info", "查询结果为空");
            object.put("status", 401);
            return object;
        }
    }

14、根据名称获取收藏数并排序

@GetMapping("/nameCount")
    public Object nameCount() {
        Map<String, Integer> collectMap = new TreeMap<String, Integer>();
        List<Collect> collectList = collectRepository.findAll();
        int count;
        for (Collect collect : collectList) {
            collectMap.put(collect.getName(), 0);
        }
        for (Collect collect : collectList) {
            count = collectMap.get(collect.getName());
            collectMap.put(collect.getName(), count + 1);
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(collectMap.entrySet());
        list.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        for (Map.Entry<String, Integer> mapping : list) {
            System.out.println(mapping.getKey() + ":" + mapping.getValue());
        }
        JSONObject object = new JSONObject();
        object.put("collectCount", list);
        object.put("status", 200);
        return object;
    }
上一篇:Java 8 - 收集器Collectors


下一篇:PHP框架Laravel学习笔记——集合的常用方法