Реализация книги

This commit is contained in:
Филиппов Александр
2017-11-30 17:51:40 +07:00
parent ebbd7c60ab
commit c42cfd18b2
6 changed files with 212 additions and 140 deletions

View File

@ -0,0 +1,31 @@
package ru.cft.task.restServer;
public class BookStatus {
private boolean status;
private String message;
public String getMessage() {
return message;
}
public BookStatus setMessage(String message) {
this.message = message;
this.status = true;
return this;
}
public BookStatus setError(String message) {
this.message = message;
this.status = false;
return this;
}
public boolean isValid() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}

View File

@ -1,41 +1,35 @@
package ru.cft.task.restServer;
import java.util.HashMap;
public class EmailBook {
private long id;
private String name;
private String email;
private HashMap<Long, EmailRecord> book;
private BookStatus status;
public EmailBook(long id, String name, String email) {
setId(id);
setName(name);
setEmail(email);
public EmailBook() {
this.book = new HashMap();
this.status.setStatus(true);
}
public long getId() {
return id;
public BookStatus addRecord(EmailRecord rec) {
if (!this.book.containsKey(rec.getId())) {
this.book.put(rec.getId(), rec);
return this.status.setMessage("Запись успешно добавлена");
}else{
return this.status.setError("Запись с таким id уже существует");
}
}
public void setId(long id) {
this.id = id;
public BookStatus removeRecord(Long id) {
if (!this.book.containsKey(id)) {
this.book.remove(id);
return this.status.setMessage("Запись успешно удалена");
}else{
return this.status.setError("Записи с таким id не существует");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString(){
return String.valueOf(getId()).concat(": ").concat(getName()).concat(" - ").concat(getEmail());
public int count() {
return this.book.size();
}
}

View File

@ -2,6 +2,7 @@ package ru.cft.task.restServer;
import org.apache.log4j.Logger;
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 java.util.concurrent.atomic.AtomicLong;
@ -10,12 +11,15 @@ import java.util.concurrent.atomic.AtomicLong;
public class EmailBookController {
final static Logger log = Logger.getLogger(EmailBookController.class);
private final AtomicLong new_id = new AtomicLong();
private EmailBook book = new EmailBook();
@RequestMapping("/email")
public EmailBook emailRec(@RequestParam(value = "name", defaultValue = "John Unknown") String name,
@RequestParam(value = "email", defaultValue = "john@unknown.ru") String email) {
EmailBook emailBook = new EmailBook(new_id.incrementAndGet(), name, email);
log.debug(emailBook.toString());
return emailBook;
@RequestMapping(value = "/email", method = RequestMethod.POST)
public BookStatus emailRec(@RequestParam(value = "name", defaultValue = "John Unknown") String name,
@RequestParam(value = "email", defaultValue = "john@unknown.ru") String email) {
EmailRecord rec = new EmailRecord(new_id.incrementAndGet(), name, email);
return this.book.addRecord(rec);
}
//@RequestMapping(value = "/email", method = RequestMethod.DELETE)
//public em
}

View File

@ -0,0 +1,41 @@
package ru.cft.task.restServer;
public class EmailRecord {
private long id;
private String name;
private String email;
public EmailRecord(long id, String name, String email) {
setId(id);
setName(name);
setEmail(email);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return String.valueOf(getId()).concat(": ").concat(getName()).concat(" - ").concat(getEmail());
}
}