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;
|
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
|
@RestController
|
||||||
|
@RequestMapping("/api/v1")
|
||||||
public class MemberController {
|
public class MemberController {
|
||||||
|
@Autowired
|
||||||
|
private MemberRepository memberRepository;
|
||||||
|
|
||||||
@GetMapping("/employees")
|
@GetMapping("/members")
|
||||||
public List<Member> all() {
|
public List<Member> getAllMembers() {
|
||||||
return repository.findAll();
|
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 {
|
||||||
|
Member member = memberRepository.findById(memberId)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("Member not found for this id :: " + memberId));
|
||||||
|
|
||||||
return repository.findById(id)
|
member.setEmailId(memberDetails.getEmailId());
|
||||||
.map(employee -> {
|
member.setLastName(memberDetails.getLastName());
|
||||||
employee.setName(newMember.getName());
|
member.setFirstName(memberDetails.getFirstName());
|
||||||
employee.setRole(newMember.getRole());
|
final Member updatedMember = memberRepository.save(member);
|
||||||
return repository.save(employee);
|
return ResponseEntity.ok(updatedMember);
|
||||||
})
|
|
||||||
.orElseGet(() -> {
|
|
||||||
newMember.setId(id);
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user