Skip to content

Commit a73075d

Browse files
ft/recomendaciones terminado + extract methods + exception
1 parent 1a7a218 commit a73075d

File tree

52 files changed

+183
-119
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+183
-119
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.outfitlab.project.domain.exceptions;
2+
3+
public class GarmentNotFoundException extends Exception {
4+
public GarmentNotFoundException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.outfitlab.project.domain.interfaces.repositories;
22

3+
import com.outfitlab.project.domain.exceptions.GarmentNotFoundException;
34
import com.outfitlab.project.domain.model.GarmentRecomendationModel;
45

56
import java.util.List;
67

78
public interface GarmentRecomendationRepository {
89

9-
List<GarmentRecomendationModel> findRecomendationsByGarmentCode(String garmentCode);
10+
List<GarmentRecomendationModel> findRecomendationsByGarmentCode(String garmentCode) throws GarmentNotFoundException;
1011

1112
}

src/main/java/com/outfitlab/project/domain/useCases/garment/GetGarmentRecomendation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.outfitlab.project.domain.useCases.garment;
22

3+
import com.outfitlab.project.domain.exceptions.GarmentNotFoundException;
34
import com.outfitlab.project.domain.interfaces.repositories.GarmentRecomendationRepository;
45
import com.outfitlab.project.domain.model.GarmentRecomendationModel;
56

@@ -13,7 +14,7 @@ public GetGarmentRecomendation(GarmentRecomendationRepository garmentRecomendati
1314
this.garmentRecomendationRepository = garmentRecomendationRepository;
1415
}
1516

16-
public List<GarmentRecomendationModel> execute(String garmentCode) {
17+
public List<GarmentRecomendationModel> execute(String garmentCode) throws GarmentNotFoundException {
1718
return this.garmentRecomendationRepository.findRecomendationsByGarmentCode(garmentCode);
1819
}
1920
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.outfitlab.project.infrastructure.config;
2+
3+
import com.outfitlab.project.domain.interfaces.repositories.GarmentRecomendationRepository;
4+
import com.outfitlab.project.domain.useCases.garment.GetGarmentRecomendation;
5+
import com.outfitlab.project.infrastructure.repositories.RecomendationRepository;
6+
import com.outfitlab.project.infrastructure.repositories.interfaces.GarmentJpaRepository;
7+
import com.outfitlab.project.infrastructure.repositories.interfaces.RecomendationJpaRepository;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
@Configuration
12+
public class RecomendationConfig {
13+
14+
@Bean
15+
public GarmentRecomendationRepository RecomendationRepository(RecomendationJpaRepository recomendationJpaRepository, GarmentJpaRepository garmentJpaRepository) {
16+
return new RecomendationRepository(recomendationJpaRepository, garmentJpaRepository);
17+
}
18+
19+
@Bean
20+
public GetGarmentRecomendation getGarmentRecomendation(GarmentRecomendationRepository garmentRecomendationRepository){
21+
return new GetGarmentRecomendation(garmentRecomendationRepository);
22+
}
23+
}

src/main/java/com/outfitlab/project/infrastructure/model/PrendaEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class PrendaEntity {
2929
@Column(nullable = false)
3030
private String imagenUrl;
3131

32-
@Column(nullable = false, unique = true)
32+
@Column(unique = true)
3333
private String garmentCode;
3434

3535
@ManyToOne(fetch = FetchType.LAZY)
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.outfitlab.project.infrastructure.repositories;
22

3+
import com.outfitlab.project.domain.exceptions.GarmentNotFoundException;
34
import com.outfitlab.project.domain.interfaces.repositories.GarmentRecomendationRepository;
45
import com.outfitlab.project.domain.model.GarmentRecomendationModel;
56
import com.outfitlab.project.infrastructure.model.GarmentRecomendationEntity;
67
import com.outfitlab.project.infrastructure.model.PrendaEntity;
78
import com.outfitlab.project.infrastructure.repositories.interfaces.GarmentJpaRepository;
89
import com.outfitlab.project.infrastructure.repositories.interfaces.RecomendationJpaRepository;
10+
import org.jetbrains.annotations.NotNull;
911

1012
import java.util.List;
1113
import java.util.stream.Collectors;
@@ -21,20 +23,29 @@ public RecomendationRepository(RecomendationJpaRepository recomendationJpaReposi
2123
}
2224

2325
@Override
24-
public List<GarmentRecomendationModel> findRecomendationsByGarmentCode(String garmentCode) {
25-
PrendaEntity garment = this.garmentJpaRepository.findByGarmentCode(garmentCode);
26+
public List<GarmentRecomendationModel> findRecomendationsByGarmentCode(String garmentCode) throws GarmentNotFoundException {
27+
return getGarmentRecomendationEntities(getPrendaEntity(garmentCode)).stream()
28+
.map(GarmentRecomendationEntity::convertToModel)
29+
.collect(Collectors.toList());
30+
}
2631

32+
private List<GarmentRecomendationEntity> getGarmentRecomendationEntities(PrendaEntity garment) {
2733
List<GarmentRecomendationEntity> recomendations;
2834

29-
if (garment.getTipo().equalsIgnoreCase("SUPERIOR")) {
30-
recomendations = this.recomendationJpaRepository.findByTopGarment(garment.getId()); // si es superior, bsco combinaciones buscando por superior
31-
} else{
32-
recomendations = this.recomendationJpaRepository.findByBottomGarment(garment.getId()); // y si es inferior, al revéz, busco combinaciones por inferior
33-
}
35+
if (garment.getTipo().equalsIgnoreCase("SUPERIOR"))
36+
recomendations = this.recomendationJpaRepository.findByTopGarment(garment); // si es superior, bsco combinaciones buscando por superior
37+
else
38+
recomendations = this.recomendationJpaRepository.findByBottomGarment(garment); // y si es inferior, al revéz, busco combinaciones por inferior
3439

35-
return recomendations.stream()
36-
.map(GarmentRecomendationEntity::convertToModel)
37-
.collect(Collectors.toList());
40+
return recomendations;
41+
}
42+
43+
@NotNull
44+
private PrendaEntity getPrendaEntity(String garmentCode) throws GarmentNotFoundException {
45+
PrendaEntity garment = this.garmentJpaRepository.findByGarmentCode(garmentCode);
46+
47+
if (garment == null) throw new GarmentNotFoundException("No encontramos la prenda con el código: " + garmentCode);
48+
return garment;
3849
}
3950

4051
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.outfitlab.project.infrastructure.repositories.interfaces;
22

33
import com.outfitlab.project.infrastructure.model.GarmentRecomendationEntity;
4+
import com.outfitlab.project.infrastructure.model.PrendaEntity;
45
import org.springframework.data.jpa.repository.JpaRepository;
56

67
import java.util.List;
78

89
public interface RecomendationJpaRepository extends JpaRepository<GarmentRecomendationEntity, Long> {
910

10-
List<GarmentRecomendationEntity> findByTopGarment(Long garmentId);
11-
List<GarmentRecomendationEntity> findByBottomGarment(Long garmentId);
11+
List<GarmentRecomendationEntity> findByTopGarment(PrendaEntity garment);
12+
List<GarmentRecomendationEntity> findByBottomGarment(PrendaEntity garment);
1213

1314
}

src/main/java/com/outfitlab/project/presentation/RecomendationController.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.outfitlab.project.presentation;
22

33
import com.outfitlab.project.domain.useCases.garment.GetGarmentRecomendation;
4-
import com.outfitlab.project.infrastructure.repositories.RecomendationRepository;
5-
import lombok.AllArgsConstructor;
64
import org.springframework.http.ResponseEntity;
75
import org.springframework.web.bind.annotation.GetMapping;
86
import org.springframework.web.bind.annotation.PathVariable;
Binary file not shown.

0 commit comments

Comments
 (0)