Skip to content

Commit

Permalink
Merge pull request #99 from gfa-cc-after/SCRUM-82
Browse files Browse the repository at this point in the history
Scrum 82
  • Loading branch information
AnnaUgrai authored Sep 7, 2024
2 parents 13f7f59 + 0851821 commit 5bd2fe9
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 8 deletions.
4 changes: 4 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@
<artifactId>spring-boot-starter-mail</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

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

/**
* This is the main class of the application.
*/
@SpringBootApplication
@EnableCaching
public class BackendApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.greenfoxacademy.backend.config;

import java.time.Duration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* RedisConfig is a configuration class that defines the cache management strategy
* for Redis in the Spring Boot application. It configures the Redis Cache Manager
* and sets key serialization and expiration policies.
*/
@Configuration
@ConditionalOnProperty(name = "spring.cache.type", havingValue = "redis")
public class RedisConfig {

/**
* The cacheManager method creates a RedisCacheManager bean that is used to manage.
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(3)) // Set TTL to 5 minutes
.disableCachingNullValues()
.serializeKeysWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(new StringRedisSerializer())
);

return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(cacheConfig)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
* @param token the token that is returned to identify the user
*/

public record LoginResponseDto(
String token
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
import com.greenfoxacademy.backend.services.auth.AuthService;
import com.greenfoxacademy.backend.services.mail.EmailService;
import jakarta.transaction.Transactional;

import java.util.UUID;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand Down Expand Up @@ -47,17 +51,16 @@ public RegisterResponseDto register(RegisterRequestDto registerRequestDto)
try {
User saved = userRepository.save(user);
emailService.sendRegistrationEmail(
saved.getEmail(),
saved.getFirstName(),
saved.getVerificationId()
saved.getEmail(),
saved.getFirstName(),
saved.getVerificationId()
);
return new RegisterResponseDto(saved.getId());
} catch (Exception e) {
throw new UserAlreadyExistsError("Email is already taken!");
}
}


@Override
public LoginResponseDto login(LoginRequestDto loginRequestDto) throws Exception {
User user = userRepository.findByEmail(loginRequestDto.email())
Expand All @@ -70,6 +73,7 @@ public LoginResponseDto login(LoginRequestDto loginRequestDto) throws Exception
return new LoginResponseDto(authService.generateToken(user));
}

@CacheEvict(value = "update-profile-cache", key = "#email")
@Override
public ProfileUpdateResponseDto profileUpdate(
String email,
Expand All @@ -79,7 +83,7 @@ public ProfileUpdateResponseDto profileUpdate(
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
if (
userRepository.existsByEmail(profileUpdateRequestDto.email())
&& !email.equals(profileUpdateRequestDto.email())
&& !email.equals(profileUpdateRequestDto.email())
) {
throw new CannotUpdateUserException("Email is already taken!");
}
Expand All @@ -92,6 +96,7 @@ public ProfileUpdateResponseDto profileUpdate(
return new ProfileUpdateResponseDto(authService.generateToken(updatedUser));
}

@Cacheable(value = "profile-cache", key = "#username")
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return userRepository.findByEmail(username)
Expand All @@ -101,6 +106,7 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
/**
* Delete the user by username.
*/
@CacheEvict(value = "delete-cache", key = "#username")
@Transactional
@Override
public void deleteProfile(String username) {
Expand Down
8 changes: 7 additions & 1 deletion backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

# Where the verification link will redirect
email.baseUrl=${EMAIL_BASEURL:http://localhost:8080}
email.baseUrl=${EMAIL_BASEURL:http://localhost:8080}

# Redis configuration
spring.data.redis.host=${CACHE_URL:localhost}
spring.data.redis.port=${CACHE_PORT:6379}
spring.data.redis.password=${CACHE_PASSWORD}
spring.cache.type=redis
5 changes: 4 additions & 1 deletion backend/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ spring.mail.properties.mail.smtp.starttls.enable=true
cors.urls=${CORS_URLS:http://localhost:5173}

# Where the verification link will redirect
email.baseUrl=${EMAIL_BASEURL:http://localhost:8080}
email.baseUrl=${EMAIL_BASEURL:http://localhost:8080}

# Redis configuration
spring.cache.type=none
11 changes: 11 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ services:
POSTGRES_PASSWORD: petclinic_db_password
POSTGRES_DB: petclinic_db

redis-cache:
image: bitnami/redis
environment:
- REDIS_PASSWORD=some_redis_password
ports:
- 6379:6379

frontend:
build:
context: ./frontend
Expand All @@ -28,5 +35,9 @@ services:
- DATABASE_USER=petclinic_db_user
- DATABASE_PASSWORD=petclinic_db_password
- CORS_URLS=http://localhost:5173
- CACHE_PORT=6379
- CACHE_URL=redis://redis-cache
- CACHE_PASSWORD=some_redis_password
depends_on:
- postgres-db
- redis-cache
2 changes: 1 addition & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#create frontend container
FROM node:lts as builder
FROM node:lts AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
Expand Down

0 comments on commit 5bd2fe9

Please sign in to comment.