Skip to content

Commit

Permalink
feat: #186 change dao to store
Browse files Browse the repository at this point in the history
  • Loading branch information
KartVen committed Nov 27, 2024
1 parent ce2c0f7 commit 9abd14f
Show file tree
Hide file tree
Showing 78 changed files with 675 additions and 2,270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import pl.sknikod.kodemybackend.infrastructure.common.lan.LanNetworkHandler;
import pl.sknikod.kodemycommons.exception.handler.RestExceptionHandler;
import pl.sknikod.kodemycommons.network.LanRestTemplate;
import pl.sknikod.kodemycommons.security.JwtProvider;

@Configuration
public class WebConfiguration {

@Bean
public RestExceptionHandler restExceptionHandler(){
return new RestExceptionHandler();
}

@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
Expand All @@ -24,15 +29,8 @@ public RestTemplate restTemplate() {
}

@Bean
public LanNetworkHandler lanNetworkHandler(
LanNetworkProperties lanNetworkProperties,
JwtProvider jwtProvider,
@Value("${network.route.auth}") String authRouteBaseUrl
) {
LanRestTemplate lanRestTemplate = new LanRestTemplate(
lanNetworkProperties.connectTimeoutMs, lanNetworkProperties.readTimeoutMs, jwtProvider
);
return new LanNetworkHandler(lanRestTemplate, authRouteBaseUrl);
public LanRestTemplate lanRestTemplate(LanNetworkProperties lanNetworkProperties, JwtProvider jwtProvider) {
return new LanRestTemplate(lanNetworkProperties.connectTimeoutMs, lanNetworkProperties.readTimeoutMs, jwtProvider);
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pl.sknikod.kodemybackend.infrastructure.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AfterAction {
Action action();

enum Action {
SAVE,
UPDATE,
STATUS_UPDATE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package pl.sknikod.kodemybackend.infrastructure.aspect;

import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.control.Try;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemybackend.infrastructure.database.Material;
import pl.sknikod.kodemybackend.infrastructure.event.producer.MaterialCreatedProducer;
import pl.sknikod.kodemybackend.infrastructure.event.producer.MaterialStatusUpdatedProducer;
import pl.sknikod.kodemybackend.infrastructure.event.producer.MaterialUpdatedProducer;
import pl.sknikod.kodemybackend.infrastructure.store.GradeStore;
import pl.sknikod.kodemybackend.infrastructure.store.UserStore;

@Aspect
@Component
@RequiredArgsConstructor
@Slf4j
public class AfterActionAspect {
private final GradeStore gradeStore;
private final UserStore userStore;
private final MaterialCreatedProducer materialCreatedProducer;
private final MaterialUpdatedProducer materialUpdatedProducer;
private final MaterialStatusUpdatedProducer materialStatusUpdatedProducer;

@Pointcut("@annotation(afterAction)")
public void afterActionPointcut(AfterAction afterAction) {
// placeholder
}

@SuppressWarnings("unchecked")
@AfterReturning(value = "afterActionPointcut(afterAction)", returning = "result", argNames = "afterAction,result")
public void executeAfterAction(AfterAction afterAction, Object result) {
switch (afterAction.action()) {
case SAVE -> afterSave((Try<Material>) result);
case UPDATE -> afterUpdate((Try<Material>) result);
case STATUS_UPDATE -> afterStatusUpdate((Tuple2<Long, Material.MaterialStatus>) result);
}
}

private void afterSave(Try<Material> result) {
log.info("After save action executed");
result
.filter(material -> Material.MaterialStatus.APPROVED.equals(material.getStatus()))
.mapTry(material -> {
UserStore.User user = userStore.findById(material.getUserId()).get();
Double avgGrade = gradeStore.findAvgGradeByMaterial(material.getUserId()).get();
return Tuple.of(material, avgGrade, user);
})
.mapTry(tuple -> MaterialCreatedProducer.Message.map(tuple._1, tuple._3.getUsername()))
.peek(materialCreatedProducer::publish);
}

private void afterUpdate(Try<Material> result) {
log.info("After update action executed");
result.filter(material -> Material.MaterialStatus.APPROVED.equals(material.getStatus()))
.mapTry(material -> {
UserStore.User user = userStore.findById(material.getUserId()).get();
Double avgGrade = gradeStore.findAvgGradeByMaterial(material.getUserId()).get();
return Tuple.of(material, avgGrade, user);
})
.mapTry(tuple -> MaterialUpdatedProducer.Message.map(tuple._1, tuple._2, tuple._3.getUsername()))
.peek(materialUpdatedProducer::publish);
}

private void afterStatusUpdate(Tuple2<Long, Material.MaterialStatus> result) {
log.info("After status update action executed");
Try.of(() -> new MaterialStatusUpdatedProducer.Message(result._1, result._2))
.peek(materialStatusUpdatedProducer::publish);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public class Grade extends BaseEntity {
@JoinColumn(name = "material_id", nullable = false)
private Material material;

public Grade(Double value, Long userId, Long materialId) {
this.value = value;
this.userId = userId;
this.material = new Material();
this.material.setId(materialId);
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -49,4 +51,8 @@ Page<Object[]> searchMaterialsWithAvgGrades(
Double maxAvgGrade,
Pageable pageable
);

@Modifying
@Query(value = "UPDATE materials SET status = :newStatus where id = :materialId", nativeQuery = true)
void updateStatus(Long materialId, Material.MaterialStatus newStatus);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.sknikod.kodemybackend.infrastructure.event;

public interface Producer<T> {
void publish(T object);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package pl.sknikod.kodemybackend.infrastructure.module.material.producer;
package pl.sknikod.kodemybackend.infrastructure.event.producer;

import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemybackend.infrastructure.database.Material;
import pl.sknikod.kodemybackend.infrastructure.event.Producer;
import pl.sknikod.kodemycommons.security.UserPrincipal;

import java.time.Instant;
Expand Down Expand Up @@ -58,7 +59,7 @@ public static class Author {
String username;
}

public static Message map(Material material, UserPrincipal userPrincipal) {
public static Message map(Material material, String username) {
final var category = material.getCategory();
return new Message(
material.getId(),
Expand All @@ -67,7 +68,7 @@ public static Message map(Material material, UserPrincipal userPrincipal) {
material.getStatus(),
material.isActive(),
0.00,
new Author(userPrincipal.getId(), userPrincipal.getUsername()),
new Author(material.getUserId(), username),
material.getCreatedDate()
.toInstant(ZoneId.systemDefault().getRules().getOffset(material.getCreatedDate())),
category.getSection().getId(),
Expand Down
Loading

0 comments on commit 9abd14f

Please sign in to comment.