Skip to content

Commit d17d4b4

Browse files
author
HoyoenKim
committed
[#3] create meal no test
1 parent 075da06 commit d17d4b4

File tree

6 files changed

+68
-76
lines changed

6 files changed

+68
-76
lines changed

src/main/java/com/example/helper/dto/MealWrapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55

66
@Getter
77
public class MealWrapper {
8+
public MealWrapper(Meal meal) {
9+
this.meal = meal;
10+
}
811
private Meal meal;
912
}

src/main/java/com/example/helper/dto/Mealdto.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@
88
@Getter
99
@Setter
1010
public class Mealdto {
11-
12-
1311
public Mealdto(String title, String meal_date, String kind_of_meal, String menu) {
1412
this.title = title;
1513
this.meal_date = meal_date;
1614
this.kind_of_meal = kind_of_meal;
1715
this.menu = menu;
1816
}
19-
2017
private String title;
2118
private String meal_date;
2219
private String kind_of_meal;
2320
private String menu;
24-
2521
}

src/main/java/com/example/helper/repository/MealRepository.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import com.example.helper.entity.Meal;
44

5+
import java.util.Optional;
6+
57
public interface MealRepository {
68
Meal save(Meal meal);
79

8-
Long findIdByDateTitleKind(String title, String meal_date, String kind_of_meal);
10+
Optional<Meal> findIdByDateTitleKind(String title, String meal_date, String kind_of_meal);
911

10-
Meal findById(Long mealId);
12+
Optional<Meal> findById(Long mealId);
1113

1214
Long delete(Long mealId);
1315
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.helper.repository;
2+
3+
import com.example.helper.entity.Meal;
4+
import jakarta.persistence.EntityManager;
5+
import org.springframework.stereotype.Repository;
6+
7+
import java.util.List;
8+
import java.util.Optional;
9+
10+
@Repository
11+
public class SqlMealRepository implements MealRepository {
12+
private final EntityManager em;
13+
public SqlMealRepository(EntityManager em) {
14+
this.em = em;
15+
}
16+
@Override
17+
public Meal save(Meal meal) {
18+
em.persist(meal);
19+
return meal;
20+
}
21+
22+
@Override
23+
public Optional<Meal> findIdByDateTitleKind(String title, String meal_date, String kind_of_meal) {
24+
List<Meal> result = em.createQuery("select m from Member m where m.title = :title and m.meal_date = :meal_date and m.kind_of_meal = :kind_of_meal", Meal.class)
25+
.setParameter("title", title)
26+
.setParameter("meal_date", meal_date)
27+
.setParameter("kind_of_meal", kind_of_meal)
28+
.getResultList();
29+
return result.stream().findAny();
30+
}
31+
32+
@Override
33+
public Optional<Meal> findById(Long mealId) {
34+
Meal meal = em.find(Meal.class, mealId);
35+
return Optional.ofNullable(meal);
36+
}
37+
38+
@Override
39+
public Long delete(Long mealId) {
40+
return null;
41+
}
42+
}

src/main/java/com/example/helper/repository/SqliteMealRepository.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/main/java/com/example/helper/service/MealService.java

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.example.helper.entity.Meal;
44
import com.example.helper.repository.MealRepository;
5+
import com.example.helper.repository.SqlMealRepository;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.stereotype.Service;
78

@@ -10,31 +11,22 @@ public class MealService {
1011

1112
@Autowired(required = true)
1213
private MealRepository mealRepository;
13-
//
14-
// public Meal save(Meal mael) {
15-
// Meal n = new Meal();
16-
//
17-
// n.setTitle(meal.getTitle());
18-
// n.setStart(reqMeal.getStart());
19-
// n.setEnd(reqMeal.getEnd());
20-
// n.setUser(reqMeal.getUser());
21-
//
22-
// // 중복 모르겠고 일단 다 넣자.
23-
//// if (
24-
//// !timetableService.validateDay(reqTime.getDay())
25-
//// || !timetableService.validateTime(reqTime.getStart(), reqTime.getEnd())
26-
//// || timetableService.isDuplicateNew(n, newBuildingTimetableRepository.findByDay(reqTime.getDay()))
27-
//// ) {
28-
//// Throwable ex = new Throwable();
29-
//// throw new ResponseStatusException(
30-
//// HttpStatus.NOT_ACCEPTABLE, "Invalid day, time", ex);
31-
//// }
32-
//// mealRepository.save(n);
33-
// }
34-
//
35-
// public Meal findByMeal(Meal meal) {
36-
// Meal result = new Meal();
37-
// Integer target = mealRepository.findByDateTitlealfajflkfdj()
38-
//
39-
// }
14+
15+
public MealService(MealRepository mealRepository) {
16+
this.mealRepository = mealRepository;
17+
}
18+
19+
public Long mealCreate(Meal meal) {
20+
// 중복 체크는 없음
21+
validateDuplicateMeal(meal);
22+
mealRepository.save(meal);
23+
return meal.getId();
24+
}
25+
26+
private void validateDuplicateMeal(Meal meal) {
27+
mealRepository.findIdByDateTitleKind(meal.getTitle(), meal.getMeal_date(), meal.getKind_of_meal())
28+
.ifPresent(m -> {
29+
throw new IllegalStateException("이미 존재하는 회원입니다.");
30+
});
31+
}
4032
}

0 commit comments

Comments
 (0)