MerchantsController Restful接口修改

This commit is contained in:
myh 2023-10-25 18:15:56 +08:00
parent 8180141994
commit b09082f30a

View File

@ -3,12 +3,10 @@ package com.example.springdemo.controller;
import com.example.springdemo.entities.Merchants; import com.example.springdemo.entities.Merchants;
import com.example.springdemo.service.MerchantsService; import com.example.springdemo.service.MerchantsService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import java.util.Optional;
@RestController @RestController
@RequestMapping("/merchants") @RequestMapping("/merchants")
@ -16,34 +14,33 @@ public class MerchantsController {
@Resource @Resource
private MerchantsService merchantsService; private MerchantsService merchantsService;
@GetMapping("/findAll") @PostMapping("/add")
public List<Merchants> getMerchants() { public Merchants saveMerchants(@RequestBody Merchants merchants) {
return merchantsService.findAll(); return merchantsService.insetMerchants(merchants);
} }
@GetMapping("/findByName") @DeleteMapping("/delete/{id}")
public List<Merchants> getMerchantsByName(@RequestParam("name") String name) { public void deleteMerchants(@PathVariable("id") Long id) {
merchantsService.deleteMerchantsById(id);
}
@PutMapping("/update")
public Merchants updateMerchants(@RequestBody Merchants merchants) {
return merchantsService.updateMerchants(merchants);
}
@GetMapping("/find")
public List<Merchants> getMerchants() {
return merchantsService.findAllMerchants();
}
@GetMapping("/find/{name}")
public Merchants getMerchantsByName(@PathVariable("name") String name) {
return merchantsService.findByName(name); return merchantsService.findByName(name);
} }
@GetMapping("/save") @GetMapping("/find/{id}")
public String saveMerchants(@RequestParam("name") String name, public Optional<Merchants> getMerchantsById(@PathVariable("id") Long id) {
@RequestParam("address") String address, return merchantsService.findById(id);
@RequestParam("description") String description,
@RequestParam("phoneNumber") String phoneNumber) {
Merchants merchants = new Merchants();
merchants.setName(name);
merchants.setAddress(address);
merchants.setDescription(description);
merchants.setPhoneNumber(phoneNumber);
merchantsService.save(merchants);
return "success";
}
@GetMapping("/delete")
public String deleteMerchants(@RequestParam("id") Long id) {
merchantsService.deleteById(id);
return "success";
} }
} }