Добавление метода patch

This commit is contained in:
2017-12-03 12:43:44 +07:00
parent 964f6bc028
commit 2550181534
4 changed files with 219 additions and 129 deletions

View File

@ -16,7 +16,7 @@ public class EmailBook {
public EmailRecord addEmailRecord(long id, String name, String email) throws EmailException {
for (Map.Entry<Long, EmailRecord> rec : book.entrySet()) {
if (rec.getValue().getEmail() == email) {
throw new EmailException("Запись с email " + email + " уже есть в базе");
throw new EmailException("Запись с {email = " + email + "} уже есть в базе");
}
}
EmailRecord new_rec = new EmailRecord(id, name, email);
@ -29,7 +29,7 @@ public class EmailBook {
book.remove(id);
return true;
}
throw new EmailException("Запись с id = " + id + " не найдена");
throw new EmailException("Запись с {id = " + id + "} не найдена");
}
public int count() {
@ -40,7 +40,7 @@ public class EmailBook {
if (book.containsKey(id)) {
return book.get(id);
}
throw new EmailException("Запись с id = " + id + " не найдена");
throw new EmailException("Запись с {id = " + id + "} не найдена");
}
public EmailRecord findRecordByName(String name) throws EmailException {
@ -49,7 +49,7 @@ public class EmailBook {
return rec.getValue();
}
}
throw new EmailException("Запись с именем " + name + " не найдена");
throw new EmailException("Запись с {name = " + name + "} не найдена");
}
public EmailRecord findRecordByEmail(String email) throws EmailException {
@ -58,6 +58,19 @@ public class EmailBook {
return rec.getValue();
}
}
throw new EmailException("Запись с email " + email + " не найдена");
throw new EmailException("Запись с {email = " + email + "} не найдена");
}
public EmailRecord editRecord(long id, String name, String email) throws EmailException {
if (book.containsKey(id)) {
if (!name.isEmpty()) {
book.get(id).setName(name);
}
if (!email.isEmpty()) {
book.get(id).setEmail(email);
}
return book.get(id);
}
throw new EmailException("Запись с {id = " + id + "} не найдена");
}
}

View File

@ -22,17 +22,6 @@ public class EmailBookController {
return new ResponseEntity<EmailRecord>(emailBook.addEmailRecord(new_id.incrementAndGet(), name, email), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<ErrorResponse> removeEmailRec(@RequestParam(value = "id", required = true) long id) throws EmailException {
if (emailBook.removeEmailRecord(id)) {
ErrorResponse message = new ErrorResponse();
message.setErrorCode(HttpStatus.OK.value());
message.setMessage("Запись {id = " + id + "} успешно удалена");
return new ResponseEntity<ErrorResponse>(message, HttpStatus.OK);
}
return null;
}
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<EmailRecord> findEmailRec(@RequestParam(value = "id", defaultValue = "") String id,
@RequestParam(value = "name", defaultValue = "") String name,
@ -47,6 +36,25 @@ public class EmailBookController {
}
}
@RequestMapping(method = RequestMethod.PATCH)
public ResponseEntity<EmailRecord> editEmailRec(@RequestParam(value = "id", required = true) long id,
@RequestParam(value = "name", defaultValue = "") String name,
@RequestParam(value = "email", defaultValue = "") String email
) throws EmailException {
return new ResponseEntity<EmailRecord>(emailBook.editRecord(id, name, email), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<ErrorResponse> removeEmailRec(@RequestParam(value = "id", required = true) long id) throws EmailException {
if (emailBook.removeEmailRecord(id)) {
ErrorResponse message = new ErrorResponse();
message.setErrorCode(HttpStatus.OK.value());
message.setMessage("Запись {id = " + id + "} успешно удалена");
return new ResponseEntity<ErrorResponse>(message, HttpStatus.OK);
}
return null;
}
@RequestMapping(value = "/count", method = RequestMethod.GET)
public int countEmailRecords() {
return emailBook.count();

View File

@ -2,3 +2,5 @@
,'_/ / __//_ _/
/ /_ / _/ / /
|__/ /_/ /_/
${application.title} ${application.formatted-version}