Skip to content

Commit 7fee65d

Browse files
authored
Merge pull request #87 from MYONGSIK/infra/mongodb
2 parents 3200fd0 + db3ea92 commit 7fee65d

18 files changed

+431
-0
lines changed

β€Žbuild.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ dependencies {
2929
annotationProcessor 'org.projectlombok:lombok'
3030
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.7.4'
3131

32+
// mongo
33+
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
34+
3235
implementation group: 'io.swagger.core.v3', name: 'swagger-annotations', version: '2.2.2'
3336
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
3437

β€Ždocker-compose.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: "3"
2+
services:
3+
mongodb:
4+
image: mongo
5+
container_name: mongo_test
6+
ports:
7+
- "27017:27017"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.example.myongsick.domain.dish.controller;
2+
3+
import com.example.myongsick.domain.dish.dto.DishRequest;
4+
import com.example.myongsick.domain.dish.dto.DishResponse;
5+
import com.example.myongsick.domain.dish.service.DishService;
6+
import com.example.myongsick.domain.dish.dto.HateRequest;
7+
import com.example.myongsick.domain.dish.dto.LoveRequest;
8+
import com.example.myongsick.domain.dish.dto.RestaurantRequest;
9+
import com.example.myongsick.global.object.ApplicationResponse;
10+
import java.util.List;
11+
import lombok.RequiredArgsConstructor;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.PutMapping;
15+
import org.springframework.web.bind.annotation.RequestBody;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
@RestController
20+
@RequiredArgsConstructor
21+
@RequestMapping("/api/v3/dish")
22+
public class DishController {
23+
24+
private final DishService dishService;
25+
26+
@GetMapping
27+
public ApplicationResponse<List<DishResponse>> getDishAll() {
28+
return ApplicationResponse.ok(dishService.getDishAll());
29+
}
30+
31+
@PostMapping
32+
public ApplicationResponse<String> createDish(
33+
@RequestBody DishRequest request
34+
) {
35+
return ApplicationResponse.ok(dishService.createDish(request));
36+
}
37+
38+
@PostMapping("/restaurant")
39+
public ApplicationResponse<String> createRestaurant(
40+
@RequestBody RestaurantRequest request
41+
) {
42+
return ApplicationResponse.ok(dishService.createRestaurant(request));
43+
}
44+
45+
@PutMapping("/love")
46+
public ApplicationResponse<Integer> calculateLoveCount(
47+
@RequestBody LoveRequest request
48+
) {
49+
return ApplicationResponse.ok(dishService.calculateLoveCount(request));
50+
}
51+
@PutMapping("/hate")
52+
public ApplicationResponse<Integer> calculateHateCount(
53+
@RequestBody HateRequest request
54+
) {
55+
return ApplicationResponse.ok(dishService.calculateHateCount(request));
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.myongsick.domain.dish.dto;
2+
3+
import java.time.LocalDate;
4+
import java.util.List;
5+
import lombok.Getter;
6+
7+
@Getter
8+
public class DishRequest {
9+
private LocalDate offeredAt;
10+
private String mealType;
11+
private String statusType;
12+
private String restaurantId;
13+
private List<String> menu;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.myongsick.domain.dish.dto;
2+
3+
import com.example.myongsick.domain.dish.entity.Dish;
4+
import java.time.format.DateTimeFormatter;
5+
import java.util.List;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Builder;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
11+
@Getter
12+
@Builder
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
public class DishResponse {
16+
private String dishId;
17+
private String toDay;
18+
private String mealType;
19+
private String statusType;
20+
private List<String> menu;
21+
22+
public static DishResponse toDto(Dish dish) {
23+
return DishResponse.builder()
24+
.dishId(dish.get_id())
25+
.toDay(dish.getOfferedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
26+
.mealType(dish.getMealType().name())
27+
.statusType(dish.getStatusType().name())
28+
.menu(dish.getMenus())
29+
.build();
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.myongsick.domain.dish.dto;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class HateRequest {
7+
8+
private String userId;
9+
private String dishId;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.myongsick.domain.dish.dto;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class LoveRequest {
7+
8+
private String userId;
9+
private String dishId;
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.myongsick.domain.dish.dto;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class RestaurantRequest {
7+
8+
private String name;
9+
private String campusType;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.example.myongsick.domain.dish.entity;
2+
3+
import com.example.myongsick.domain.meal.entity.MealType;
4+
import com.example.myongsick.domain.meal.entity.StatusType;
5+
import com.fasterxml.jackson.annotation.JsonFormat;
6+
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
7+
import java.time.LocalDate;
8+
import java.util.List;
9+
import lombok.Builder;
10+
import lombok.Getter;
11+
import lombok.NoArgsConstructor;
12+
import org.springframework.data.annotation.Id;
13+
import org.springframework.data.mongodb.core.mapping.Document;
14+
import org.springframework.data.mongodb.core.mapping.Field;
15+
import org.springframework.data.mongodb.core.mapping.FieldType;
16+
17+
@Getter
18+
@NoArgsConstructor
19+
@Document
20+
public class Dish {
21+
22+
@Id
23+
@Field(value = "_id", targetType = FieldType.OBJECT_ID)
24+
private String _id;
25+
26+
@Field("dish_type")
27+
private MealType mealType;
28+
29+
@Field("status_type")
30+
private StatusType statusType;
31+
32+
private List<String> menus;
33+
34+
@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
35+
private LocalDate offeredAt;
36+
37+
private Integer loveCount;
38+
private Integer hateCount;
39+
40+
private String restaurantId;
41+
42+
@Builder
43+
public Dish(
44+
String mealType,
45+
String statusType,
46+
List<String> menus,
47+
LocalDate offeredAt,
48+
String restaurantId
49+
) {
50+
this.mealType = MealType.valueOf(mealType);
51+
this.statusType = StatusType.valueOf(statusType);
52+
this.menus = menus;
53+
this.offeredAt = offeredAt;
54+
this.restaurantId = restaurantId;
55+
this.loveCount= 0;
56+
this.hateCount = 0;
57+
}
58+
public void addLoveCount() {
59+
this.loveCount++;
60+
System.out.println("loveCount ->>>>>> " + loveCount);
61+
}
62+
public void subLoveCount() {
63+
this.loveCount--;
64+
}
65+
public void addHateCount() {
66+
this.hateCount++;
67+
}
68+
public void subHateCount() {
69+
this.hateCount--;
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.myongsick.domain.dish.entity;
2+
3+
import lombok.Builder;
4+
import org.springframework.data.annotation.Id;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import org.springframework.data.mongodb.core.mapping.Document;
8+
import org.springframework.data.mongodb.core.mapping.Field;
9+
import org.springframework.data.mongodb.core.mapping.FieldType;
10+
11+
@Getter
12+
@NoArgsConstructor
13+
@Document
14+
public class Hate {
15+
@Id
16+
@Field(value = "_id", targetType = FieldType.OBJECT_ID)
17+
private String _id;
18+
19+
private String userId;
20+
21+
private String dishId;
22+
23+
@Builder
24+
public Hate(
25+
String userId,
26+
String dishId
27+
) {
28+
this.userId = userId;
29+
this.dishId = dishId;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.myongsick.domain.dish.entity;
2+
3+
import org.springframework.data.annotation.Id;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import org.springframework.data.mongodb.core.mapping.Document;
8+
import org.springframework.data.mongodb.core.mapping.Field;
9+
import org.springframework.data.mongodb.core.mapping.FieldType;
10+
11+
@Getter
12+
@NoArgsConstructor
13+
@Document
14+
public class Love {
15+
16+
@Id
17+
@Field(value = "_id", targetType = FieldType.OBJECT_ID)
18+
private String _id;
19+
20+
private String userId;
21+
22+
private String dishId;
23+
24+
@Builder
25+
public Love(
26+
String userId,
27+
String dishId
28+
) {
29+
this.userId = userId;
30+
this.dishId = dishId;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.myongsick.domain.dish.entity;
2+
3+
import com.example.myongsick.domain.scrap.entity.CampusType;
4+
5+
import org.springframework.data.annotation.Id;
6+
import lombok.Builder;
7+
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
import org.springframework.data.mongodb.core.mapping.Document;
10+
import org.springframework.data.mongodb.core.mapping.Field;
11+
import org.springframework.data.mongodb.core.mapping.FieldType;
12+
13+
@Getter
14+
@NoArgsConstructor
15+
@Document
16+
public class Restaurant {
17+
18+
@Id
19+
@Field(value = "_id", targetType = FieldType.OBJECT_ID)
20+
private String _id;
21+
22+
@Field("name")
23+
private String name;
24+
25+
private CampusType campusType;
26+
27+
@Builder
28+
public Restaurant(
29+
String name,
30+
String campusType
31+
) {
32+
this.name = name;
33+
this.campusType = CampusType.valueOf(campusType);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.myongsick.domain.dish.repository;
2+
3+
import com.example.myongsick.domain.dish.entity.Dish;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface DishRepository extends MongoRepository<Dish, String> {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.myongsick.domain.dish.repository;
2+
3+
import com.example.myongsick.domain.dish.entity.Hate;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
6+
public interface HateRepository extends MongoRepository<Hate, String> {
7+
Boolean existsByDishIdAndUserId(String dishId, String userId);
8+
void deleteByDishIdAndUserId(String dishId, String userId);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.myongsick.domain.dish.repository;
2+
3+
import com.example.myongsick.domain.dish.entity.Love;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
6+
public interface LoveRepository extends MongoRepository<Love, String> {
7+
Boolean existsByDishIdAndUserId(String dishId, String userId);
8+
void deleteByDishIdAndUserId(String dishId, String userId);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.myongsick.domain.dish.repository;
2+
3+
import com.example.myongsick.domain.dish.entity.Restaurant;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
6+
public interface RestaurantRepository extends MongoRepository<Restaurant, String> {
7+
8+
}

0 commit comments

Comments
Β (0)