From 03185625873239cbc055d0ad91d7f4ebb089a693 Mon Sep 17 00:00:00 2001 From: csmuxa <37396594+csmuxa@users.noreply.github.com> Date: Fri, 24 May 2019 21:38:26 +0300 Subject: [PATCH] Database --- .../io/repositories/AddressRepository.java | 14 +++++ .../iu/model/response/AddressesRest.java | 59 +++++++++++++++++++ .../project/service/AddressService.java | 12 ++++ .../serviceImpl/AddressServiceImpl.java | 48 +++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 src/main/java/com/springProject/project/io/repositories/AddressRepository.java create mode 100644 src/main/java/com/springProject/project/iu/model/response/AddressesRest.java create mode 100644 src/main/java/com/springProject/project/service/AddressService.java create mode 100644 src/main/java/com/springProject/project/service/serviceImpl/AddressServiceImpl.java diff --git a/src/main/java/com/springProject/project/io/repositories/AddressRepository.java b/src/main/java/com/springProject/project/io/repositories/AddressRepository.java new file mode 100644 index 0000000..5e67347 --- /dev/null +++ b/src/main/java/com/springProject/project/io/repositories/AddressRepository.java @@ -0,0 +1,14 @@ +package com.springProject.project.io.repositories; + +import com.springProject.project.io.entity.AddressEntity; +import com.springProject.project.io.entity.UserEntity; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +@Repository +public interface AddressRepository extends CrudRepository { + List findAllByUserDetails(UserEntity userEntity); + + AddressEntity findByAddressId(String addressId); +} diff --git a/src/main/java/com/springProject/project/iu/model/response/AddressesRest.java b/src/main/java/com/springProject/project/iu/model/response/AddressesRest.java new file mode 100644 index 0000000..1dafc3d --- /dev/null +++ b/src/main/java/com/springProject/project/iu/model/response/AddressesRest.java @@ -0,0 +1,59 @@ +package com.springProject.project.iu.model.response; + + +public class AddressesRest { + private String addressId; + private String country; + private String city; + private String streetName; + private String postalCode; + private String type; + + public String getAddressId() { + return addressId; + } + + public void setAddressId(String addressId) { + this.addressId = addressId; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getStreetName() { + return streetName; + } + + public void setStreetName(String streetName) { + this.streetName = streetName; + } + + public String getPostalCode() { + return postalCode; + } + + public void setPostalCode(String postalCode) { + this.postalCode = postalCode; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/src/main/java/com/springProject/project/service/AddressService.java b/src/main/java/com/springProject/project/service/AddressService.java new file mode 100644 index 0000000..eccd9c8 --- /dev/null +++ b/src/main/java/com/springProject/project/service/AddressService.java @@ -0,0 +1,12 @@ +package com.springProject.project.service; + +import com.springProject.project.shared.dto.AddressDto; + +import java.util.List; + +public interface AddressService { + List getAddresses(String userId); + + AddressDto getAddress(String addressId); + +} diff --git a/src/main/java/com/springProject/project/service/serviceImpl/AddressServiceImpl.java b/src/main/java/com/springProject/project/service/serviceImpl/AddressServiceImpl.java new file mode 100644 index 0000000..9edba39 --- /dev/null +++ b/src/main/java/com/springProject/project/service/serviceImpl/AddressServiceImpl.java @@ -0,0 +1,48 @@ +package com.springProject.project.service.serviceImpl; + +import com.springProject.project.UserRepository; +import com.springProject.project.io.entity.AddressEntity; +import com.springProject.project.io.entity.UserEntity; +import com.springProject.project.io.repositories.AddressRepository; +import com.springProject.project.service.AddressService; +import com.springProject.project.shared.dto.AddressDto; +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +@Service +public class AddressServiceImpl implements AddressService { + + @Autowired + UserRepository userRepository; + + @Autowired + AddressRepository addressRepository; + + @Override + public List getAddresses(String userId) { + List returnValue = new ArrayList<>(); + ModelMapper mapper = new ModelMapper(); + UserEntity userEntity = userRepository.findByUserId(userId); + if (userEntity == null) + return returnValue; + + Iterable addresses = addressRepository.findAllByUserDetails(userEntity); + for (AddressEntity addressEntity : addresses) { + returnValue.add(mapper.map(addressEntity, AddressDto.class)); + } + + return returnValue; + } + + @Override + public AddressDto getAddress(String addressId) { + AddressDto returnValue=new AddressDto(); + AddressEntity addressEntity=addressRepository.findByAddressId(addressId); + if (addressEntity!=null) + returnValue=new ModelMapper().map(addressEntity,AddressDto.class); + return returnValue; + } +}