-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
568f4e1
commit 31870d1
Showing
61 changed files
with
1,346 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
customer/src/main/java/com/yas/customer/config/DatabaseAutoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.yas.customer.config; | ||
|
||
import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.domain.AuditorAware; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
import java.util.Optional; | ||
|
||
@Configuration | ||
@EnableJpaRepositories("com.yas.customer.repository") | ||
@EntityScan({"com.yas.customer.model"}) | ||
@EnableJpaAuditing(auditorAwareRef = "auditorAware") | ||
public class DatabaseAutoConfig { | ||
|
||
@Bean | ||
public AuditorAware<String> auditorAware() { | ||
return () -> { | ||
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
if (auth == null) return Optional.of(""); | ||
return Optional.of(auth.getName()); | ||
}; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
customer/src/main/java/com/yas/customer/controller/UserAddressController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.yas.customer.controller; | ||
|
||
import com.yas.customer.service.UserAddressService; | ||
import com.yas.customer.viewmodel.AddressVm; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class UserAddressController { | ||
|
||
private final UserAddressService userAddressService; | ||
|
||
@GetMapping("/storefront/user-address") | ||
public ResponseEntity<List<Long>> getUserAddresses() { | ||
return ResponseEntity.ok(userAddressService.getUserAddressList()); | ||
} | ||
|
||
@PostMapping("/storefront/user-address/{id}") | ||
public ResponseEntity createAddress(@PathVariable Long id) { | ||
userAddressService.createAddress(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@DeleteMapping("/storefront/user-address/{id}") | ||
public ResponseEntity deleteAddress(@PathVariable Long id) { | ||
userAddressService.deleteAddress(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
customer/src/main/java/com/yas/customer/listener/CustomAuditingEntityListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.yas.customer.listener; | ||
|
||
import com.yas.customer.model.AbstractAuditEntity; | ||
import jakarta.persistence.PrePersist; | ||
import jakarta.persistence.PreUpdate; | ||
import org.springframework.beans.factory.ObjectFactory; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.auditing.AuditingHandler; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
@Configuration | ||
public class CustomAuditingEntityListener extends AuditingEntityListener { | ||
public CustomAuditingEntityListener(ObjectFactory<AuditingHandler> handler) { | ||
super.setAuditingHandler(handler); | ||
} | ||
|
||
@Override | ||
@PrePersist | ||
public void touchForCreate(Object target) { | ||
AbstractAuditEntity entity = (AbstractAuditEntity) target; | ||
if (entity.getCreatedBy() == null) { | ||
super.touchForCreate(target); | ||
} else { | ||
if (entity.getLastModifiedBy() == null) { | ||
entity.setLastModifiedBy(entity.getCreatedBy()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
@PreUpdate | ||
public void touchForUpdate(Object target) { | ||
AbstractAuditEntity entity = (AbstractAuditEntity) target; | ||
if (entity.getLastModifiedBy() == null) { | ||
super.touchForUpdate(target); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
customer/src/main/java/com/yas/customer/model/AbstractAuditEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.yas.customer.model; | ||
|
||
import com.yas.customer.listener.CustomAuditingEntityListener; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
import org.hibernate.annotations.UpdateTimestamp; | ||
import org.springframework.data.annotation.CreatedBy; | ||
import org.springframework.data.annotation.LastModifiedBy; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
@MappedSuperclass | ||
@Getter | ||
@Setter | ||
@EntityListeners(CustomAuditingEntityListener.class) | ||
public class AbstractAuditEntity { | ||
|
||
@CreationTimestamp | ||
private ZonedDateTime createdOn; | ||
|
||
@CreatedBy | ||
private String createdBy; | ||
|
||
@UpdateTimestamp | ||
private ZonedDateTime lastModifiedOn; | ||
|
||
@LastModifiedBy | ||
private String lastModifiedBy; | ||
} |
37 changes: 37 additions & 0 deletions
37
customer/src/main/java/com/yas/customer/model/UserAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.yas.customer.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
import org.hibernate.annotations.UpdateTimestamp; | ||
import org.springframework.data.annotation.CreatedBy; | ||
import org.springframework.data.annotation.LastModifiedBy; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
@Entity | ||
@Table(name = "user_address") | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class UserAddress extends AbstractAuditEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String userId; | ||
|
||
private Long addressId; | ||
|
||
private Boolean isActive; | ||
} |
12 changes: 12 additions & 0 deletions
12
customer/src/main/java/com/yas/customer/repository/UserAddressRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.yas.customer.repository; | ||
|
||
import com.yas.customer.model.UserAddress; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface UserAddressRepository extends JpaRepository<UserAddress, Long> { | ||
List<UserAddress> findAllByUserId(String userId); | ||
|
||
UserAddress findOneByUserIdAndAddressId(String userId, Long id); | ||
} |
47 changes: 47 additions & 0 deletions
47
customer/src/main/java/com/yas/customer/service/UserAddressService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.yas.customer.service; | ||
|
||
import com.yas.customer.exception.NotFoundException; | ||
import com.yas.customer.model.UserAddress; | ||
import com.yas.customer.repository.UserAddressRepository; | ||
import com.yas.customer.utils.Constants; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class UserAddressService { | ||
private final UserAddressRepository userAddressRepository; | ||
|
||
public List<Long> getUserAddressList() { | ||
String userId = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
|
||
List<UserAddress> userAddressList = userAddressRepository.findAllByUserId(userId); | ||
return userAddressList.stream() | ||
.map(UserAddress::getAddressId) | ||
.toList(); | ||
} | ||
|
||
public void createAddress(Long addressId) { | ||
String userId = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
UserAddress userAddress = UserAddress.builder() | ||
.userId(userId) | ||
.addressId(addressId) | ||
.isActive(false) | ||
.build(); | ||
|
||
userAddressRepository.save(userAddress); | ||
} | ||
|
||
public void deleteAddress(Long id) { | ||
String userId = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
UserAddress userAddress = userAddressRepository.findOneByUserIdAndAddressId(userId, id); | ||
if (userAddress == null) { | ||
throw new NotFoundException(Constants.ERROR_CODE.USER_ADDRESS_NOT_FOUND); | ||
} | ||
userAddressRepository.delete(userAddress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
customer/src/main/java/com/yas/customer/viewmodel/AddressVm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.yas.customer.viewmodel; | ||
|
||
public record AddressVm(Long id, String contactName, String phone, String addressLine1) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
customer/src/main/resources/db/changelog/data/changelog-0001.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--liquibase formatted sql |
5 changes: 5 additions & 0 deletions
5
customer/src/main/resources/db/changelog/db.changelog-master.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
databaseChangeLog: | ||
- includeAll: | ||
path: db/changelog/ddl/ | ||
- includeAll: | ||
path: db/changelog/data/ |
1 change: 1 addition & 0 deletions
1
customer/src/main/resources/db/changelog/ddl/changelog-0001.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
create table user_address (id bigserial not null, created_by varchar(255), created_on timestamp(6), last_modified_by varchar(255), last_modified_on timestamp(6), user_id varchar(255) not null, address_id bigserial not null, is_active boolean, primary key(id)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
USER_WITH_EMAIL_NOT_FOUND = User with email {} not found | ||
WRONG_EMAIL_FORMAT = Wrong email format for {} | ||
USER_NOT_FOUND = User not found | ||
USER_WITH_EMAIL_NOT_FOUND=User with email {} not found | ||
WRONG_EMAIL_FORMAT=Wrong email format for {} | ||
USER_NOT_FOUND=User not found | ||
USER_ADDRESS_NOT_FOUND=User address not found |
13 changes: 0 additions & 13 deletions
13
customer/src/test/java/com/yas/customer/CustomerApplicationTests.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package service; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CustomerServiceTest { | ||
@BeforeEach | ||
void setUp() { | ||
|
||
} | ||
|
||
@Test | ||
void Test() { | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
location/src/main/java/com/yas/location/controller/AddressController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.yas.location.controller; | ||
|
||
import com.yas.location.mapper.AddressResponseMapper; | ||
import com.yas.location.service.AddressService; | ||
import com.yas.location.viewmodel.address.AddressGetVm; | ||
import com.yas.location.viewmodel.address.AddressPostVm; | ||
import com.yas.location.viewmodel.address.RequestAddressGetListVm; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class AddressController { | ||
private final AddressService addressService; | ||
|
||
@PostMapping("/storefront/address") | ||
public ResponseEntity<Long> createAddress(@RequestBody AddressPostVm dto) { | ||
return ResponseEntity.ok(addressService.createAddress(dto).getId()); | ||
} | ||
|
||
@PutMapping("/storefront/address/{id}") | ||
public ResponseEntity updateAddress(@PathVariable Long id, @RequestBody AddressPostVm dto) { | ||
addressService.updateAddress(id, dto); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@GetMapping("/storefront/address/{id}") | ||
public ResponseEntity<AddressGetVm> getAddress(@PathVariable Long id) { | ||
return ResponseEntity.ok(addressService.getAddress(id)); | ||
} | ||
|
||
@PostMapping("/storefront/addresses") | ||
public ResponseEntity<List<AddressResponseMapper>> getAddressList(@RequestBody RequestAddressGetListVm dto) { | ||
return ResponseEntity.ok(addressService.getAddressList(dto.ids())); | ||
} | ||
|
||
@DeleteMapping("/storefront/address/{id}") | ||
public ResponseEntity deleteAddress(@PathVariable Long id) { | ||
addressService.deleteAddress(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
Oops, something went wrong.