From b09082f30a3348f2ba4d0caa5730e0a1ada76ebd Mon Sep 17 00:00:00 2001 From: myh Date: Wed, 25 Oct 2023 18:15:56 +0800 Subject: [PATCH] =?UTF-8?q?MerchantsController=20Restful=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MerchantsController.java | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/example/springdemo/controller/MerchantsController.java b/src/main/java/com/example/springdemo/controller/MerchantsController.java index b84c0d5..30d1a39 100644 --- a/src/main/java/com/example/springdemo/controller/MerchantsController.java +++ b/src/main/java/com/example/springdemo/controller/MerchantsController.java @@ -3,12 +3,10 @@ package com.example.springdemo.controller; import com.example.springdemo.entities.Merchants; import com.example.springdemo.service.MerchantsService; import jakarta.annotation.Resource; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; @RestController @RequestMapping("/merchants") @@ -16,34 +14,33 @@ public class MerchantsController { @Resource private MerchantsService merchantsService; - @GetMapping("/findAll") - public List getMerchants() { - return merchantsService.findAll(); + @PostMapping("/add") + public Merchants saveMerchants(@RequestBody Merchants merchants) { + return merchantsService.insetMerchants(merchants); } - @GetMapping("/findByName") - public List getMerchantsByName(@RequestParam("name") String name) { + @DeleteMapping("/delete/{id}") + 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 getMerchants() { + return merchantsService.findAllMerchants(); + } + + @GetMapping("/find/{name}") + public Merchants getMerchantsByName(@PathVariable("name") String name) { return merchantsService.findByName(name); } - @GetMapping("/save") - public String saveMerchants(@RequestParam("name") String name, - @RequestParam("address") String address, - @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"; + @GetMapping("/find/{id}") + public Optional getMerchantsById(@PathVariable("id") Long id) { + return merchantsService.findById(id); } }