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 0000000..9e3a170 Binary files /dev/null and b/target/classes/com/loafle/totopia/commons/exception/ResourceNotFoundException.class differ diff --git a/target/classes/com/loafle/totopia/member/controller/MemberController.class b/target/classes/com/loafle/totopia/member/controller/MemberController.class index a87f5be..0a402ba 100644 Binary files a/target/classes/com/loafle/totopia/member/controller/MemberController.class and b/target/classes/com/loafle/totopia/member/controller/MemberController.class differ