ing
This commit is contained in:
parent
8fe1e682d0
commit
1f0fa4dbbf
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<Member> all() {
|
||||
return repository.findAll();
|
||||
@RestController
|
||||
@RequestMapping("/api/v1")
|
||||
public class MemberController {
|
||||
@Autowired
|
||||
private MemberRepository memberRepository;
|
||||
|
||||
@GetMapping("/members")
|
||||
public List<Member> getAllMembers() {
|
||||
return memberRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/employees")
|
||||
public Member newMember(@RequestBody Member newMember) {
|
||||
return repository.save(newMember);
|
||||
|
||||
@GetMapping("/members/{id}")
|
||||
public ResponseEntity<Member> 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<Member> 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<String, Boolean> 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<String, Boolean> response = new HashMap<>();
|
||||
response.put("deleted", Boolean.TRUE);
|
||||
return response;
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user