当前位置:网站首页>[Ruiji takeout] day05 package management business development
[Ruiji takeout] day05 package management business development
2022-07-28 22:20:00 【ajjjjjjjjjtty】
Catalog
- New package
- Paging query of package information
- Delete package
New package _ Demand analysis & Data model
Demand analysis
- A set meal is a collection of dishes
- Package information can be managed in the background system , Add a new package through the new package function
- When adding a package, you need Select the Package classification and Dishes included , And need Upload the picture corresponding to the package
- On the mobile end Show the corresponding packages according to the package classification
Data model
- New package , In fact, that is Insert the package information entered on the new page into setmeal surface , We also need to setmeal_dish surface Insert set meal and dish Association data
- So when adding a package , It involves two tables :
- setmeal---- Package list
- setmeal_dish---- Relationship table of package dishes
New package _ Code development _ preparation & Sort out the interaction process
Code development - preparation
Before developing business functions , First, create the basic structure of classes and interfaces that need to be used :
- Entity class SetmealDish( Import directly from the course materials ,Setmeal The entity has been imported in the previous course )
- DTO SetmealDto ( Import directly from the course materials )
- Mapper Interface SetmealDishMapper
- Business layer interface SetmealDishService
- Business layer implementation classes SetmealDishservicelmpl
- Control layer SetmealController
Code development - Sort out the interaction process
Before developing code , You need to sort out the interaction process between the front-end page and the server when adding a package :
- page (backend/page/combo/add.html) send out ajax request , Request server Get the package classification data and display it in the drop-down box ( Completed )
- Page sending ajax request , Request server , Get dish classification data And show it in the add dishes window
- Page sending ajax request , Request server , Query the corresponding dish data according to the dish classification And show it in the add dishes window
- The page sends a request for pictures Upload , Request the server to save the picture to the server ( Completed )
- The page sends a request for pictures download , Echo the uploaded picture ( Completed )
- Click the save button , send out ajax request , The data related to the package is displayed in json Submit to the server in the form of
Develop new package functions , In fact, it is to write code on the server side to process the information sent by the front-end page 6 One request is enough
New package _ Code development _ Query dishes according to classification
Front end analysis
Start project , Enter package management , Click new package , You will find that the request sent by the page is not received by the server
Explosion system interface is abnormal , The server does not define the method of querying dishes
Related codes
stay DishController Class , add to list Method
Be careful : Additional query criteria need to be added , Query only status by 1 The data of , Indicates that the dish is in the starting status , Can be added to the package , For user selection
@GetMapping("/list")
public R<List<Dish>> list(Dish dish){
// Construct query conditions
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
// Adding conditions , The query status is 1(1 For initial sale ,0 To stop selling ) The dishes
queryWrapper.eq(Dish::getStatus,1);
List<Dish> list = dishService.list(queryWrapper);
// Add sorting criteria
return R.success(list);
}
New package _ Code development _ The server receives the data submitted by the page
Front end analysis
Start project , Go to the add package page , input data , Click save
View the request sent by the front page , Request mode
Related codes ( The beta )
stay SetmealController Class save Method
Implementation class SetmealServicelmpl, Implement the method of interface addition , And add code logic to the method
- Save the basic information of the package
- Save the related information of packages and dishes
@Override
@Transactional
public void saveWithDish(SetmealDto setmealDto) {
// Save the basic information of the package , operation setmeal, perform insert operation
save(setmealDto);
List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
setmealDishes = setmealDishes.stream().map((item) -> {
item.setSetmealId(setmealDto.getId());
return item;
}).collect(Collectors.toList());
// Save the related information of packages and dishes
setmealDishService.saveBatch(setmealDishes);
}
stay SetmealController The control layer save In the method , call saveWithDish Method , Save data to database
@PostMapping
public R<String> save(@RequestBody SetmealDto setmealDto){
log.info(" Data transmission object setmealDto:{}",setmealDto.toString());
setmealService.saveWithDish(setmealDto);
return R.success(" Successfully added package ");
}
Paging query of package information _ Demand analysis & Sort out the interaction process
Demand analysis
- When there are a lot of package data in the system , If you show it all in one page, it will look messy , Not easy to view
- In general, the list data will be displayed in the way of paging
Sort out the interaction process
Before developing code , You need to sort out the interaction process between the front-end page and the server during package paging query :
- page (backend/page/combo/list.html) send out ajax request , Paging query parameters (page、pageSize、name) Submit to the server , Get paging data
- Page send request , Request the server to download pictures , For page image display
Develop the paging query function of package information , In fact, it is to write code on the server side to process the information sent by the front-end page 2 One request is enough
Paging query of package information _ Code development & A functional test
Code development
SetmealController Class , add to list Method
@GetMapping("/page")
public R<Page> list(int page,int pageSize,String name){
// Page builder object
Page<Setmeal> pageInfo = new Page<>(page, pageSize);
Page<SetmealDto> dtoPage = new Page<>();
// Construct query criteria object
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(name != null, Setmeal::getName, name);
// Operating the database
setmealService.page(pageInfo,queryWrapper);
// Object Copy
BeanUtils.copyProperties(pageInfo,dtoPage,"records");
List<Setmeal> records = pageInfo.getRecords();
List<SetmealDto> list = records.stream().map((item) -> {
SetmealDto setmealDto = new SetmealDto();
BeanUtils.copyProperties(item, setmealDto);
// obtain categoryId
Long categoryId = item.getCategoryId();
Category category = categoryService.getById(categoryId);
if (category != null) {
String categoryName = category.getName();
setmealDto.setCategoryName(categoryName);
}
return setmealDto;
}).collect(Collectors.toList());
dtoPage.setRecords(list);
return R.success(dtoPage);
}
Be careful
In the package management interface , The package category field displays categoryId Corresponding Chinese , But what you find in the database is categoryId, So we need to use categoryId Query to categoryName, And assign it to the data transmission object SetmealDto
Delete package _ Demand analysis & Sort out the interaction process
Demand analysis
- Click the delete button on the package management list page , You can delete the corresponding package information
- You can also select multiple packages through the check box , Click the batch delete button to delete multiple packages at one time
- Be careful , Packages in sale status cannot be deleted , Need to stop selling , Then delete .
Sort out the interaction process
Before developing code , You need to sort out the interaction process between the front-end page and the server when deleting the package :
- When deleting a single package , Page sending ajax request , According to the package id Delete the corresponding package
- When deleting multiple packages , Page sending ajax request , According to multiple packages submitted id Delete the corresponding package development delete package function
In fact, it is to write code on the server side to process the information sent by the front-end page 2 One request is enough
- By observing the request information of deleting single packages and batch packages, we can find , The address and request method of the two requests are the same Of
- The difference is transmitted id Number , Therefore, a method can be provided on the server side to handle .
Delete package _ Code development & A functional test
Code development ( test )
stay SetmealController Add delete Method
@DeleteMapping
public R<String> delete(@RequestParam List<Long> ids){
log.info("ids by :",ids);
return null;
}Code development perfect version
1. stay SetmealService Add to interface removeWithDish Method
2. stay SetmealServicelmpl Implement the methods added in the corresponding interface in the implementation class
@Override
@Transactional
public void removeWithDish(List<Long> ids) {
//select count(*) from setmeal where ids in(1,2,3) and status = 1
// Query package status , Determine if you can delete
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(Setmeal::getId,ids);
queryWrapper.eq(Setmeal::getStatus,1);
int count = super.count(queryWrapper);
if (count > 0){
// If you can't delete , Throw a business exception
throw new CustomException(" The package is on sale , Can't delete ");
}
// If you can delete , First delete the data in the package table
super.removeByIds(ids);
// Delete data in relation table
//delete from setmeal_dish where setmeal_id in(1,2,3)
LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
dishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
setmealDishService.remove(dishLambdaQueryWrapper);
}
stay SetmealController Improve the code — call removeWithDish Method , Successfully delete the package data
@DeleteMapping
public R<String> delete(@RequestParam List<Long> ids){
log.info("ids by :",ids);
setmealService.removeWithDish(ids);
return R.success(" Package data deleted successfully ");
}
边栏推荐
- Embrace open source guidelines
- HCIP第七次实验
- The person in charge of Tencent cloud database borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
- Basic introduction of Rockwell AB PLC rslogix digital quantity IO module
- ssh 免密码登录
- C语言编程规范学习笔记和总结
- 39. Combined sum
- MOV格式是不是静态图像文件格式
- 04.toRef 默认值
- Have you seen the management area decoupling architecture? Can help customers solve big problems
猜你喜欢

JS DOM编程之平平无奇小练习

Bugku, Web: all filtered

Aimbetter insight into your database, DPM and APM solutions

从 Web3到Web2.5,是倒退还是另辟蹊径?

DHCP and PPPoE protocols and packet capture analysis

Apifox: satisfy all your fantasies about API

2021数学建模B组练习

Hcip experiment (12)

Add DNS server to LAN for domain name resolution

Alibaba cloud CDN practice
随机推荐
Part 8: creating camera classes
ECMASript 5/6 笔记
HCIP(11)
Intranet penetration learning (III) horizontal movement of domain - planning tasks
成立不到一年!MIT衍生量子计算公司完成900万美元融资
From Web3 to web2.5, is it backward or another way?
HCIP(14)
The binary search boundary value processing based on leetcode35 is used to clarify the boundary value of the judgment condition using the idea of interval
40. Combined sum II
Necessary for in-depth learning: split the data set, split the labels according to the split pictures, and check the interval of all marked labels
[binary tree] pseudo palindrome path in binary tree
小程序 监听目标节点出现到屏幕中
HCIP(9)
Byte side: can TCP and UDP use the same port?
MOV格式是不是静态图像文件格式
How to establish a decentralized community in Web3
Jmeter 安装第三方插件 Plugins Manager
Bugku,Web:都过滤了
AimBetter洞察您的数据库,DPM 和 APM 解决方案
乌官员:乌克兰一半农产品经多瑙河港口出口