Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
kiettrangthuan committed Apr 3, 2023
1 parent 568f4e1 commit 31870d1
Show file tree
Hide file tree
Showing 61 changed files with 1,346 additions and 21 deletions.
13 changes: 13 additions & 0 deletions customer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@
<artifactId>commons-validator</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
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());
};
}
}
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();
}
}
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);
}
}
}
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 customer/src/main/java/com/yas/customer/model/UserAddress.java
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;
}
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);
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public final class ERROR_CODE {
public static final String USER_WITH_EMAIL_NOT_FOUND = "USER_WITH_EMAIL_NOT_FOUND";
public static final String WRONG_EMAIL_FORMAT = "WRONG_EMAIL_FORMAT";
public static final String USER_NOT_FOUND = "USER_NOT_FOUND";
public static final String USER_ADDRESS_NOT_FOUND = "USER_ADDRESS_NOT_FOUND";
}
}
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) {
}
14 changes: 14 additions & 0 deletions customer/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-

spring.security.oauth2.resourceserver.jwt.issuer-uri=http://identity/realms/Yas

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/customer
spring.datasource.username=admin
spring.datasource.password=admin

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

# Hibernate ddl auto (none, create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = none

#Enable liquibase
spring.liquibase.enabled=true

keycloak.auth-server-url=http://identity
keycloak.realm=Yas
keycloak.resource=customer-management
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--liquibase formatted sql
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/
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))
7 changes: 4 additions & 3 deletions customer/src/main/resources/messages/messages.properties
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

This file was deleted.

15 changes: 15 additions & 0 deletions customer/src/test/java/service/CustomerServiceTest.java
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() {
}
}
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();
}
}
Loading

0 comments on commit 31870d1

Please sign in to comment.