Skip to content

Commit

Permalink
ep 8
Browse files Browse the repository at this point in the history
  • Loading branch information
aware-natthaponp committed Mar 27, 2021
1 parent 081ad59 commit b988c02
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 13 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ at my YouTube channel https://www.youtube.com/c/NatthaponPinyo
- Kafka
- Kafka (Producer & Consumer)

## EP 8 (20 March 2021)
## EP 8 (27 March 2021)
- Angular Frontend (Activation Page)
- Angular Frontend (i18n)
- Redis

## EP 9 (3 April 2021)
- Build Spring Boot as Container (Build Image)
- Run Spring Boot from Image (Build Container)
- Introduction to Kubernetes

## EP 10 (10 April 2021)
- Build Single Node Kubernetes Clutser (Local)
- Deploy Spring Boot App (Contianerized) to Kubernetes Cluster
- Monitoring our Kubernetes Cluster

# Stay tuned for more episodes...
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.passay</groupId>
<artifactId>passay</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableCaching
public class BackendApplication {

public static void main(String[] args) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/iamnbty/training/backend/api/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.iamnbty.training.backend.business.UserBusiness;
import com.iamnbty.training.backend.exception.BaseException;
import com.iamnbty.training.backend.exception.UserException;
import com.iamnbty.training.backend.model.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,6 +19,18 @@ public UserApi(UserBusiness business) {
this.business = business;
}

@GetMapping("/profile")
public ResponseEntity<MUserProfile> getMyUserProfile() throws UserException {
MUserProfile response = business.getMyUserProfile();
return ResponseEntity.ok(response);
}

@PutMapping("/profile")
public ResponseEntity<MUserProfile> updateMyUserProfile(@RequestBody MUpdateUserProfileRequest request) throws UserException {
MUserProfile response = business.updateMyUserProfile(request);
return ResponseEntity.ok(response);
}

@PostMapping("/login")
public ResponseEntity<MLoginResponse> login(@RequestBody MLoginRequest request) throws BaseException {
MLoginResponse response = business.login(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.netty.util.internal.StringUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
Expand All @@ -36,6 +37,40 @@ public UserBusiness(UserService userService, TokenService tokenService, UserMapp
this.emailBusiness = emailBusiness;
}

public MUserProfile getMyUserProfile() throws UserException {
Optional<String> opt = SecurityUtil.getCurrentUserId();
if (opt.isEmpty()) {
throw UserException.unauthorized();
}

String userId = opt.get();

Optional<User> optUser = userService.findById(userId);
if (optUser.isEmpty()) {
throw UserException.notFound();
}

return userMapper.toUserProfile(optUser.get());
}

public MUserProfile updateMyUserProfile(MUpdateUserProfileRequest request) throws UserException {
Optional<String> opt = SecurityUtil.getCurrentUserId();
if (opt.isEmpty()) {
throw UserException.unauthorized();
}

String userId = opt.get();

// validate
if (ObjectUtils.isEmpty(request.getName())) {
throw UserException.updateNameNull();
}

User user = userService.updateName(userId, request.getName());

return userMapper.toUserProfile(user);
}

public MLoginResponse login(MLoginRequest request) throws BaseException {
// validate request

Expand Down Expand Up @@ -120,14 +155,14 @@ public MActivateResponse activate(MActivateRequest request) throws BaseException
}

public void resendActivationEmail(MResendActivationEmailRequest request) throws BaseException {
String email = request.getEmail();
if (StringUtil.isNullOrEmpty(email)) {
throw UserException.resendActivationEmailNoEmail();
String token = request.getToken();
if (StringUtil.isNullOrEmpty(token)) {
throw UserException.resendActivationEmailNoToken();
}

Optional<User> opt = userService.findByEmail(email);
Optional<User> opt = userService.findByToken(token);
if (opt.isEmpty()) {
throw UserException.resendActivationEmailNotFound();
throw UserException.resendActivationTokenNotFound();
}

User user = opt.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.io.Serializable;

@EqualsAndHashCode(callSuper = true)
@Data
@Entity(name = "m_address")
public class Address extends BaseEntity {
public class Address extends BaseEntity implements Serializable {

@Column(length = 120)
private String line1;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/iamnbty/training/backend/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@Entity(name = "m_user")
public class User extends BaseEntity {
public class User extends BaseEntity implements Serializable {

@Column(nullable = false, unique = true, length = 60)
private String email;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public static UserException createNameNull() {
return new UserException("create.name.null");
}

// UPDATE

public static UserException updateNameNull() {
return new UserException("update.name.null");
}

// LOGIN

public static UserException loginFailEmailNotFound() {
Expand Down Expand Up @@ -74,11 +80,11 @@ public static UserException activateTokenExpire() {

// RESEND ACTIVATION EMAIL

public static UserException resendActivationEmailNoEmail() {
return new UserException("resend.activation.no.email");
public static UserException resendActivationEmailNoToken() {
return new UserException("resend.activation.no.token");
}

public static UserException resendActivationEmailNotFound() {
public static UserException resendActivationTokenNotFound() {
return new UserException("resend.activation.fail");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.iamnbty.training.backend.entity.User;
import com.iamnbty.training.backend.model.MRegisterResponse;
import com.iamnbty.training.backend.model.MUserProfile;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface UserMapper {

MRegisterResponse toRegisterResponse(User user);

MUserProfile toUserProfile(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
@Data
public class MResendActivationEmailRequest {

private String email;
private String token;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.iamnbty.training.backend.model;

import lombok.Data;

@Data
public class MUpdateUserProfileRequest {

private String name;

}
12 changes: 12 additions & 0 deletions src/main/java/com/iamnbty/training/backend/model/MUserProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.iamnbty.training.backend.model;

import lombok.Data;

@Data
public class MUserProfile {

private String name;

private String email;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import com.iamnbty.training.backend.exception.BaseException;
import com.iamnbty.training.backend.exception.UserException;
import com.iamnbty.training.backend.repository.UserRepository;
import lombok.extern.log4j.Log4j2;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

@Service
@Log4j2
public class UserService {

private final UserRepository repository;
Expand All @@ -24,7 +28,9 @@ public UserService(UserRepository repository, PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}

@Cacheable(value = "user", key = "#id", unless = "#result == null")
public Optional<User> findById(String id) {
log.info("Load User From DB: " + id);
return repository.findById(id);
}

Expand All @@ -40,6 +46,7 @@ public User update(User user) {
return repository.save(user);
}

@CachePut(value = "user", key = "#id")
public User updateName(String id, String name) throws UserException {
Optional<User> opt = repository.findById(id);
if (opt.isEmpty()) {
Expand All @@ -52,10 +59,16 @@ public User updateName(String id, String name) throws UserException {
return repository.save(user);
}

@CacheEvict(value = "user", key = "#id")
public void deleteById(String id) {
repository.deleteById(id);
}

@CacheEvict(value = "user", allEntries = true)
public void deleteAll() {

}

public boolean matchPassword(String rawPassword, String encodedPassword) {
return passwordEncoder.matches(rawPassword, encodedPassword);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ spring:
enabled: true
max-file-size: 2MB
max-request-size: 5MB
redis:
host: localhost
port: 6379
cache:
type: redis
redis:
time-to-live: 300000
---
app:
token:
Expand Down

0 comments on commit b988c02

Please sign in to comment.