From 1f0fa4dbbf5995ac82773629d66e01a13bfb661e Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 17 Jul 2019 00:06:28 +0900 Subject: [PATCH] ing --- .../exception/ResourceNotFoundException.java | 14 +++ .../member/controller/MemberController.java | 90 +++++++++++------- .../exception/ResourceNotFoundException.class | Bin 0 -> 701 bytes .../member/controller/MemberController.class | Bin 2319 -> 5293 bytes 4 files changed, 68 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/loafle/totopia/commons/exception/ResourceNotFoundException.java create mode 100644 target/classes/com/loafle/totopia/commons/exception/ResourceNotFoundException.class diff --git a/src/main/java/com/loafle/totopia/commons/exception/ResourceNotFoundException.java b/src/main/java/com/loafle/totopia/commons/exception/ResourceNotFoundException.java new file mode 100644 index 0000000..a1c4452 --- /dev/null +++ b/src/main/java/com/loafle/totopia/commons/exception/ResourceNotFoundException.java @@ -0,0 +1,14 @@ +package com.loafle.totopia.commons.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class ResourceNotFoundException extends Exception{ + + private static final long serialVersionUID = 1L; + + public ResourceNotFoundException(String message){ + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/totopia/member/controller/MemberController.java b/src/main/java/com/loafle/totopia/member/controller/MemberController.java index 661a639..8752f1f 100644 --- a/src/main/java/com/loafle/totopia/member/controller/MemberController.java +++ b/src/main/java/com/loafle/totopia/member/controller/MemberController.java @@ -1,55 +1,73 @@ package com.loafle.totopia.member.controller; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import javax.validation.Valid; + +import com.loafle.totopia.commons.exception.ResourceNotFoundException; +import com.loafle.totopia.member.dao.MemberRepository; import com.loafle.totopia.member.model.Member; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -@RestController -public class MemberController { - @GetMapping("/employees") - public List all() { - return repository.findAll(); +@RestController +@RequestMapping("/api/v1") +public class MemberController { + @Autowired + private MemberRepository memberRepository; + + @GetMapping("/members") + public List getAllMembers() { + return memberRepository.findAll(); } - - @PostMapping("/employees") - public Member newMember(@RequestBody Member newMember) { - return repository.save(newMember); + + @GetMapping("/members/{id}") + public ResponseEntity getMemberById(@PathVariable(value = "id") Long memberId) + throws ResourceNotFoundException { + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new ResourceNotFoundException("Member not found for this id :: " + memberId)); + return ResponseEntity.ok().body(member); } - - // Single item - - @GetMapping("/employees/{id}") - public Member one(@PathVariable Long id) { - - return repository.findById(id) - .orElseThrow(() -> new MemberNotFoundException(id)); + + @PostMapping("/members") + public Member createMember(@Valid @RequestBody Member member) { + return memberRepository.save(member); } - - @PutMapping("/employees/{id}") - public Member replaceMember(@RequestBody Member newMember, @PathVariable Long id) { - - return repository.findById(id) - .map(employee -> { - employee.setName(newMember.getName()); - employee.setRole(newMember.getRole()); - return repository.save(employee); - }) - .orElseGet(() -> { - newMember.setId(id); - return repository.save(newMember); - }); + + @PutMapping("/members/{id}") + public ResponseEntity updateMember(@PathVariable(value = "id") Long memberId, + @Valid @RequestBody Member memberDetails) throws ResourceNotFoundException { + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new ResourceNotFoundException("Member not found for this id :: " + memberId)); + + member.setEmailId(memberDetails.getEmailId()); + member.setLastName(memberDetails.getLastName()); + member.setFirstName(memberDetails.getFirstName()); + final Member updatedMember = memberRepository.save(member); + return ResponseEntity.ok(updatedMember); } - - @DeleteMapping("/employees/{id}") - public void deleteMember(@PathVariable Long id) { - repository.deleteById(id); + + @DeleteMapping("/members/{id}") + public Map deleteMember(@PathVariable(value = "id") Long memberId) + throws ResourceNotFoundException { + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new ResourceNotFoundException("Member not found for this id :: " + memberId)); + + memberRepository.delete(member); + Map response = new HashMap<>(); + response.put("deleted", Boolean.TRUE); + return response; } } \ No newline at end of file diff --git a/target/classes/com/loafle/totopia/commons/exception/ResourceNotFoundException.class b/target/classes/com/loafle/totopia/commons/exception/ResourceNotFoundException.class new file mode 100644 index 0000000000000000000000000000000000000000..9e3a1701d5607aca7cec68ad5b5ff935e6784a46 GIT binary patch literal 701 zcmbtS$!-%t5PfaDfMb)85FlZZI3Te|^o0XbkOfMx01Xa`6JJr=uGpQ-bdRQc5`K#l z5(hqjk3zgLU=F~Ek*4-~we{C;pT7Wjg4-?B6nYb%>C~I!RCM&w=hkSNGVg+x4-?6w z^-dp1@MSTPfscJ(y6MYbo)+o~8z<(>=+wBGUOE)kf)v)IBPjwQj$ZF6s5c7TT|!6W z;>e_>G}ub@DTU@U>ul^PY~D|P0}f+h-E8NVdYbrRrbAw>E@M5mZmP{P zwmMeM6d5wN3~ak`R;^m$-d`c+G3NRe$uj=ZVE^!afB$H(hr3W*IPZgPG|(h(QLEu1 zB~FJG${kvI~rqXKmPs3 zUjYo^^(-<1XG(5aw_Rh|mU`d@ZpAY6vMeu2PcOMn;JLQV+v#v+yuOe{yFl-CW8Khg z!&%WUFI|_VfJ>3;tgN`c6}aAp!0F!D2ppWPI)PP|3zly!*>coz+`tGd z*YO3;6ABHHGN4Mj;}8pCFN5$>I(X3HE?fOo-}E;W5jZ-;DSKg zz~F*F`?zb$9NN&Mp$j>IJw?lrGgYXd$4LTkl`KnwF7dD zqi011qqZGkN=tnMgT)ZaYGB!V(eeWdbnG)qMjPY`iqwNlE9!B+Ob?{j zR0?NPkx9$z6(d-!v6wJrEK_ChgoY<@k_lGXh~OVc?l&rGCZIvi%OSXiUFc&A(KVdL zlPo9lSwK&!#+hpY8=>ve{`RPa1)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