This commit is contained in:
crusader 2019-07-17 00:06:28 +09:00
parent 8fe1e682d0
commit 1f0fa4dbbf
4 changed files with 68 additions and 36 deletions

View File

@ -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);
}
}

View File

@ -1,55 +1,73 @@
package com.loafle.totopia.member.controller; package com.loafle.totopia.member.controller;
import java.util.HashMap;
import java.util.List; 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 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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; 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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; 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.RestController;
@RestController
public class MemberController {
@GetMapping("/employees") @RestController
public List<Member> all() { @RequestMapping("/api/v1")
return repository.findAll(); public class MemberController {
@Autowired
private MemberRepository memberRepository;
@GetMapping("/members")
public List<Member> getAllMembers() {
return memberRepository.findAll();
} }
@PostMapping("/employees") @GetMapping("/members/{id}")
public Member newMember(@RequestBody Member newMember) { public ResponseEntity<Member> getMemberById(@PathVariable(value = "id") Long memberId)
return repository.save(newMember); throws ResourceNotFoundException {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new ResourceNotFoundException("Member not found for this id :: " + memberId));
return ResponseEntity.ok().body(member);
} }
// Single item @PostMapping("/members")
public Member createMember(@Valid @RequestBody Member member) {
@GetMapping("/employees/{id}") return memberRepository.save(member);
public Member one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(() -> new MemberNotFoundException(id));
} }
@PutMapping("/employees/{id}") @PutMapping("/members/{id}")
public Member replaceMember(@RequestBody Member newMember, @PathVariable Long id) { public ResponseEntity<Member> updateMember(@PathVariable(value = "id") Long memberId,
@Valid @RequestBody Member memberDetails) throws ResourceNotFoundException {
return repository.findById(id) Member member = memberRepository.findById(memberId)
.map(employee -> { .orElseThrow(() -> new ResourceNotFoundException("Member not found for this id :: " + memberId));
employee.setName(newMember.getName());
employee.setRole(newMember.getRole()); member.setEmailId(memberDetails.getEmailId());
return repository.save(employee); member.setLastName(memberDetails.getLastName());
}) member.setFirstName(memberDetails.getFirstName());
.orElseGet(() -> { final Member updatedMember = memberRepository.save(member);
newMember.setId(id); return ResponseEntity.ok(updatedMember);
return repository.save(newMember);
});
} }
@DeleteMapping("/employees/{id}") @DeleteMapping("/members/{id}")
public void deleteMember(@PathVariable Long id) { public Map<String, Boolean> deleteMember(@PathVariable(value = "id") Long memberId)
repository.deleteById(id); throws ResourceNotFoundException {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new ResourceNotFoundException("Member not found for this id :: " + memberId));
memberRepository.delete(member);
Map<String, Boolean> response = new HashMap<>();
response.put("deleted", Boolean.TRUE);
return response;
} }
} }