Skip to content

Commit

Permalink
Implement update customer api.
Browse files Browse the repository at this point in the history
  • Loading branch information
czetsuya committed Jun 23, 2019
1 parent fb9e048 commit 41d9b6e
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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.RestController;
Expand Down Expand Up @@ -63,6 +64,24 @@ public ResponseEntity<Resource<Customer>> newCustomer(@RequestBody Customer newC
return ResponseEntity.created(new URI(resource.getId().expand().getHref())).body(resource);
}

@PutMapping("/{id}")
public ResponseEntity<?> replaceCustomer(@RequestBody Customer newCustomer, @PathVariable Long id) throws URISyntaxException {

Customer updatedEmployee = repository.findById(id).map(employee -> {
employee.setName(newCustomer.getName());
employee.setAge(newCustomer.getAge());
employee.setEmail(newCustomer.getEmail());
return repository.save(employee);
}).orElseGet(() -> {
newCustomer.setId(id);
return repository.save(newCustomer);
});

Resource<Customer> resource = assembler.toResource(updatedEmployee);

return ResponseEntity.created(new URI(resource.getId().expand().getHref())).body(resource);
}

/**
* Deleted the customer from the system.client will pass the ID for the customer
* and this end point will remove customer from the system if found.
Expand Down

0 comments on commit 41d9b6e

Please sign in to comment.