5 changed files with 147 additions and 0 deletions
@ -0,0 +1,11 @@ |
|||||
|
package com.singlestone.contacts.model.dto; |
||||
|
|
||||
|
import com.singlestone.contacts.model.Address; |
||||
|
import com.singlestone.contacts.model.Name; |
||||
|
|
||||
|
public class CallListDTO { |
||||
|
|
||||
|
private Name name; |
||||
|
private String phone; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
package com.singlestone.contacts.model.dto; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
import com.singlestone.contacts.model.Address; |
||||
|
import com.singlestone.contacts.model.Name; |
||||
|
import com.singlestone.contacts.model.Phone; |
||||
|
|
||||
|
public class ContactDTO { |
||||
|
|
||||
|
private Name name; |
||||
|
private Address address; |
||||
|
private List<String> phone; |
||||
|
private String email; |
||||
|
|
||||
|
public void setName(Name name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public Name getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public void setAddress(Address address) { |
||||
|
this.address = address; |
||||
|
} |
||||
|
|
||||
|
public Address getAddress() { |
||||
|
return address; |
||||
|
} |
||||
|
|
||||
|
public void setPhone(List<Phone> phone) { |
||||
|
this.phone.clear(); |
||||
|
for (Phone p : phone) { |
||||
|
this.phone.add(p.getNumber()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public List<String> getPhone() { |
||||
|
return phone; |
||||
|
} |
||||
|
|
||||
|
public void setEmail(String email) { |
||||
|
this.email = email; |
||||
|
} |
||||
|
|
||||
|
public String getEmail() { |
||||
|
return email; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
package com.singlestone.contacts.model.dto; |
||||
|
|
||||
|
public class PhoneDTO { |
||||
|
private String number; |
||||
|
private Type type; |
||||
|
} |
||||
@ -0,0 +1,55 @@ |
|||||
|
package com.singlestone.contacts.service; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
import com.singlestone.contacts.model.Contact; |
||||
|
import com.singlestone.contacts.model.Phone; |
||||
|
import com.singlestone.contacts.model.dto.ContactDTO; |
||||
|
import com.singlestone.contacts.repository.ContactRepository; |
||||
|
import com.singlestone.contacts.repository.PhoneRepository; |
||||
|
|
||||
|
import org.modelmapper.ModelMapper; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
@Service |
||||
|
public class ContactService { |
||||
|
|
||||
|
@Autowired |
||||
|
private ContactRepository contactRepository; |
||||
|
|
||||
|
@Autowired |
||||
|
private PhoneRepository phoneRepository; |
||||
|
|
||||
|
@Autowired |
||||
|
private ModelMapper modelMapper; |
||||
|
|
||||
|
public List<ContactDTO> getAllContacts() { |
||||
|
List<Contact> contacts = contactRepository.findAll(); |
||||
|
return contacts.stream().map(this::convertToDTO).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
public void newContact(ContactDTO newContact) { |
||||
|
Contact contact = new Contact(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
private ContactDTO convertToDTO(Contact contact) { |
||||
|
ContactDTO dto = new ContactDTO(); |
||||
|
dto.setName(contact.getName()); |
||||
|
dto.setAddress(contact.getAddress()); |
||||
|
dto.setEmail(contact.getEmail()); |
||||
|
dto.setPhone(contact.getPhone()); |
||||
|
return dto; |
||||
|
} |
||||
|
|
||||
|
private Contact convertToEntity(ContactDTO contactDTO) { |
||||
|
Contact contact = new Contact(); |
||||
|
contact.setName(contactDTO.getName()); |
||||
|
contact.setAddress(contactDTO.getAddress()); |
||||
|
contact.setEmail(contactDTO.getEmail()); |
||||
|
contact.setPhone(contactDTO.getPhone().stream().map(phone -> new Phone(phone)).collect(Collectors.toList())); |
||||
|
return contact; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
-- Contacts |
||||
|
insert into CONTACTS (street, city, state, zip, email, middle, first, last) |
||||
|
values('street1', 'city1', 'state1', 'zip1', 'e1@mail.com', 'a', 'a', 'a'); |
||||
|
insert into CONTACTS (street, city, state, zip, email, middle, first, last) |
||||
|
values('street2', 'city2', 'state2', 'zip2', 'e2@mail.com', 'a', 'b', 'a'); |
||||
|
insert into CONTACTS (street, city, state, zip, email, middle, first, last) |
||||
|
values('street3', 'city3', 'state3', 'zip3', 'e3@mail.com', 'a', 'b', 'b'); |
||||
|
insert into CONTACTS (street, city, state, zip, email, middle, first, last) |
||||
|
values('street4', 'city4', 'state4', 'zip4', 'e4@mail.com', 'a', 'a', 'b'); |
||||
|
|
||||
|
-- Phones |
||||
|
insert into PHONES (number, type, contact_id) |
||||
|
values('111-111-1111', 'home', 1); |
||||
|
insert into PHONES (number, type, contact_id) |
||||
|
values('222-222-2222', 'work', 1); |
||||
|
insert into PHONES (number, type, contact_id) |
||||
|
values('333-333-3333', 'work', 2); |
||||
|
insert into PHONES (number, type, contact_id) |
||||
|
values('444-444-4444', 'home', 2); |
||||
|
insert into PHONES (number, type, contact_id) |
||||
|
values('555-555-5555', 'mobile', 3); |
||||
|
insert into PHONES (number, type, contact_id) |
||||
|
values('777-777-7777', 'work', 4); |
||||
|
insert into PHONES (number, type, contact_id) |
||||
|
values('888-888-8888', 'home', 4); |
||||
Loading…
Reference in new issue