Промежуточный коммит

This commit is contained in:
2017-12-01 21:11:21 +07:00
parent cc538bbe18
commit 1202c79210
43 changed files with 325 additions and 844 deletions

View File

@ -1,8 +1,47 @@
package ru.cft.task.restServer;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Component;
@RepositoryRestResource
public interface EmailBook extends CrudRepository{
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
@Component
public class EmailBook {
private List<EmailRecord> book;
private final AtomicLong new_id = new AtomicLong();
public EmailBook() {
book = new ArrayList<>();
}
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;
}
}
return false;
}
public int count() {
return book.size();
}
public EmailRecord findRec(long id) {
for (EmailRecord rec : book) {
if (rec.getId() == id) {
book.remove(rec);
return rec;
}
}
return null;
}
}

View File

@ -6,29 +6,26 @@ 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;
@RestController
public class EmailBookController {
private final AtomicLong new_id = new AtomicLong();
@Autowired
private EmailBook emailBook;
@RequestMapping(method = RequestMethod.POST)
public EmailRecord addEmailRec(@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 rec;
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);
}
@RequestMapping(method = RequestMethod.DELETE)
public boolean removeEmailRec(@RequestParam(value = "id", required = true) Long id) {
return true;
public boolean removeEmailRec(@RequestParam(value = "id", required = true) long id) {
return emailBook.removeEmailRecord(id);
}
@RequestMapping(method = RequestMethod.GET)
public int sizeBook() {
return 1;
public EmailRecord findEmailRec(@RequestParam(value = "id", required = true) long id) {
return emailBook.findRec(id);
}
}

View File

@ -1,29 +1,11 @@
package ru.cft.task.restServer;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class EmailRecord {
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
public EmailRecord() {
super();
}
public EmailRecord(long id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;