Реализация exception

This commit is contained in:
2017-12-02 16:04:20 +07:00
parent 1202c79210
commit 964f6bc028
8 changed files with 349 additions and 196 deletions

View File

@ -2,46 +2,62 @@ package ru.cft.task.restServer;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.HashMap;
import java.util.Map;
@Component
public class EmailBook {
private List<EmailRecord> book;
private final AtomicLong new_id = new AtomicLong();
private Map<Long, EmailRecord> book;
public EmailBook() {
book = new ArrayList<>();
book = new HashMap<>();
}
public long addEmailRecord(String name, String email) {
long id = new_id.incrementAndGet();
book.add(new EmailRecord(id, name, email));
return id;
}
public boolean removeEmailRecord(long id) {
for (EmailRecord rec : book) {
if (rec.getId() == id) {
book.remove(rec);
return true;
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 + " уже есть в базе");
}
}
return false;
EmailRecord new_rec = new EmailRecord(id, name, email);
book.put(id, new_rec);
return new_rec;
}
public boolean removeEmailRecord(long id) throws EmailException {
if (book.containsKey(id)) {
book.remove(id);
return true;
}
throw new EmailException("Запись с id = " + id + " не найдена");
}
public int count() {
return book.size();
}
public EmailRecord findRec(long id) {
for (EmailRecord rec : book) {
if (rec.getId() == id) {
book.remove(rec);
return rec;
public EmailRecord findRecordById(long id) throws EmailException {
if (book.containsKey(id)) {
return book.get(id);
}
throw new EmailException("Запись с id = " + id + " не найдена");
}
public EmailRecord findRecordByName(String name) throws EmailException {
for (Map.Entry<Long, EmailRecord> rec : book.entrySet()) {
if (rec.getValue().getName().toLowerCase() == name.toLowerCase()) {
return rec.getValue();
}
}
return null;
throw new EmailException("Запись с именем " + name + " не найдена");
}
public EmailRecord findRecordByEmail(String email) throws EmailException {
for (Map.Entry<Long, EmailRecord> rec : book.entrySet()) {
if (rec.getValue().getEmail().toLowerCase() == email.toLowerCase()) {
return rec.getValue();
}
}
throw new EmailException("Запись с email " + email + " не найдена");
}
}

View File

@ -1,31 +1,63 @@
package ru.cft.task.restServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.atomic.AtomicLong;
@RestController
@RequestMapping(value = "/")
public class EmailBookController {
private final AtomicLong new_id = new AtomicLong();
@Autowired
private EmailBook emailBook;
@RequestMapping(method = RequestMethod.POST)
public long addEmailRec(@RequestParam(value = "name", defaultValue = "John Unknown") String name,
@RequestParam(value = "email", defaultValue = "john@unknown.ru") String email) {
return emailBook.addEmailRecord(name, email);
public ResponseEntity<EmailRecord> addEmailRec(@RequestParam(value = "name", defaultValue = "John Unknown") String name,
@RequestParam(value = "email", defaultValue = "john@unknown.ru") String email) throws EmailException {
return new ResponseEntity<EmailRecord>(emailBook.addEmailRecord(new_id.incrementAndGet(), name, email), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.DELETE)
public boolean removeEmailRec(@RequestParam(value = "id", required = true) long id) {
return emailBook.removeEmailRecord(id);
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 EmailRecord findEmailRec(@RequestParam(value = "id", required = true) long id) {
return emailBook.findRec(id);
public ResponseEntity<EmailRecord> findEmailRec(@RequestParam(value = "id", defaultValue = "") String id,
@RequestParam(value = "name", defaultValue = "") String name,
@RequestParam(value = "email", defaultValue = "") String email
) throws EmailException {
if (!name.isEmpty()) {
return new ResponseEntity<EmailRecord>(emailBook.findRecordByName(name), HttpStatus.OK);
} else if (!email.isEmpty()) {
return new ResponseEntity<EmailRecord>(emailBook.findRecordByEmail(email), HttpStatus.OK);
} else {
return new ResponseEntity<EmailRecord>(emailBook.findRecordById(Long.valueOf(id)), HttpStatus.OK);
}
}
@RequestMapping(value = "/count", method = RequestMethod.GET)
public int countEmailRecords() {
return emailBook.count();
}
@ExceptionHandler({EmailException.class, Exception.class})
public ResponseEntity<ErrorResponse> exceptionHandler(Exception ex) {
ErrorResponse error = new ErrorResponse();
error.setErrorCode(HttpStatus.PRECONDITION_FAILED.value());
error.setMessage(ex.getMessage());
return new ResponseEntity<ErrorResponse>(error, HttpStatus.OK);
}
}

View File

@ -0,0 +1,13 @@
package ru.cft.task.restServer;
public class EmailException extends Exception {
private static final long serialVersionUID = 1L;
public EmailException() {
super();
}
public EmailException(String message) {
super(message);
}
}

View File

@ -37,6 +37,6 @@ public class EmailRecord {
@Override
public String toString() {
return "Rec[" + id + "]: " + name + "<" + email + ">";
return "Rec:" + name + "<" + email + ">";
}
}

View File

@ -0,0 +1,22 @@
package ru.cft.task.restServer;
public class ErrorResponse {
private int errorCode;
private String message;
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -1,3 +1,3 @@
server.port = 8090
spring.profiles.active=local
logging.level.root=DEBUG
logging.level.root=WARN

View File

@ -0,0 +1,4 @@
__ ____ _____
,'_/ / __//_ _/
/ /_ / _/ / /
|__/ /_/ /_/