Skip to content

Commit 7baeecb

Browse files
authored
Show today recipe in landing page (#318)
1 parent 8e16bbf commit 7baeecb

35 files changed

+681
-78
lines changed

pom.xml

+7-21
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>1.5.2.RELEASE</version>
17+
<version>1.5.4.RELEASE</version>
1818
</parent>
1919

2020
<properties>
@@ -24,7 +24,6 @@
2424
<assertj.version>3.0.0</assertj.version>
2525
<spring-test-dbunit.version>1.2.1</spring-test-dbunit.version>
2626
<dbunit.version>2.5.0</dbunit.version>
27-
<mockito.version>1.9.5</mockito.version>
2827
<skyscreamer.jsonassert>1.2.3</skyscreamer.jsonassert>
2928
<spring-social-google.version>1.0.0.RELEASE</spring-social-google.version>
3029
<spring-social-facebook.version>3.0.0.M1</spring-social-facebook.version>
@@ -47,6 +46,12 @@
4746
<artifactId>spring-boot-starter-web</artifactId>
4847
</dependency>
4948

49+
<dependency>
50+
<groupId>com.fasterxml.jackson.datatype</groupId>
51+
<artifactId>jackson-datatype-jdk8</artifactId>
52+
<version>${jackson.version}</version>
53+
</dependency>
54+
5055
<dependency>
5156
<groupId>org.springframework.boot</groupId>
5257
<artifactId>spring-boot-starter-data-jpa</artifactId>
@@ -141,20 +146,6 @@
141146
<scope>test</scope>
142147
</dependency>
143148

144-
<dependency>
145-
<groupId>org.skyscreamer</groupId>
146-
<artifactId>jsonassert</artifactId>
147-
<version>${skyscreamer.jsonassert}</version>
148-
<scope>test</scope>
149-
</dependency>
150-
151-
<dependency>
152-
<groupId>org.assertj</groupId>
153-
<artifactId>assertj-core</artifactId>
154-
<version>${assertj.version}</version>
155-
<scope>test</scope>
156-
</dependency>
157-
158149
<dependency>
159150
<groupId>com.github.springtestdbunit</groupId>
160151
<artifactId>spring-test-dbunit</artifactId>
@@ -167,11 +158,6 @@
167158
<version>${dbunit.version}</version>
168159
<scope>test</scope>
169160
</dependency>
170-
<dependency>
171-
<groupId>org.mockito</groupId>
172-
<artifactId>mockito-all</artifactId>
173-
<version>${mockito.version}</version>
174-
</dependency>
175161

176162
<dependency>
177163
<groupId>org.jvnet.mock-javamail</groupId>

src/main/java/com/otchi/api/ChefResource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33

44
import com.otchi.api.facades.dto.FeedDTO;
5-
import com.otchi.application.Chef;
65
import com.otchi.application.ChefProfileService;
76
import com.otchi.application.Feed;
7+
import com.otchi.domain.kitchen.Chef;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.http.HttpStatus;
1010
import org.springframework.web.bind.annotation.*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.otchi.api;
2+
3+
import com.otchi.domain.kitchen.TodayRecipe;
4+
import com.otchi.domain.kitchen.TodayRecipeRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import java.util.Optional;
10+
11+
@RestController
12+
public class TodayRecipeController {
13+
14+
private final TodayRecipeRepository todayRecipeRepository;
15+
16+
@Autowired
17+
public TodayRecipeController(TodayRecipeRepository todayRecipeRepository) {
18+
this.todayRecipeRepository = todayRecipeRepository;
19+
}
20+
21+
@GetMapping("/api/v1/today-recipe")
22+
public Optional<TodayRecipe> getTodayRecipe() {
23+
return todayRecipeRepository.findFirstByOrderByDateDesc();
24+
}
25+
}

src/main/java/com/otchi/application/Chef.java

-32
This file was deleted.

src/main/java/com/otchi/application/ChefProfileService.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.otchi.application;
22

3+
import com.otchi.domain.kitchen.Chef;
4+
35
import java.util.List;
46

57
public interface ChefProfileService {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.otchi.application;
2+
3+
import com.otchi.application.utils.Clock;
4+
import com.otchi.domain.kitchen.Chef;
5+
import com.otchi.domain.kitchen.Recipe;
6+
import com.otchi.domain.kitchen.TodayRecipe;
7+
import com.otchi.domain.kitchen.TodayRecipeRepository;
8+
import com.otchi.domain.social.models.Post;
9+
import com.otchi.domain.social.repositories.PostRepository;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.scheduling.annotation.Scheduled;
14+
import org.springframework.stereotype.Service;
15+
16+
import java.time.LocalDate;
17+
import java.time.LocalDateTime;
18+
import java.util.Optional;
19+
20+
import static java.util.Optional.of;
21+
22+
@Service
23+
public class KitchenService {
24+
25+
private final PostRepository postRepository;
26+
private final TodayRecipeRepository todayRecipeRepository;
27+
private Clock clock;
28+
private static final Logger log = LoggerFactory.getLogger(KitchenService.class);
29+
30+
31+
@Autowired
32+
public KitchenService(PostRepository postRepository, TodayRecipeRepository todayRecipeRepository, Clock clock) {
33+
this.postRepository = postRepository;
34+
this.todayRecipeRepository = todayRecipeRepository;
35+
this.clock = clock;
36+
}
37+
38+
public Optional<TodayRecipe> getTodayRecipe() {
39+
LocalDateTime now = clock.now();
40+
LocalDateTime yesterday = now.minusDays(1);
41+
Optional<Post> foundPost = postRepository.findFirstRecipePostMostLikedAfter(yesterday);
42+
return foundPost.flatMap(
43+
post -> {
44+
Recipe recipe = (Recipe) post.getPostContent();
45+
TodayRecipe todayRecipe = new TodayRecipe(post.getId(), post.images().get(0), recipe.getTitle(), new Chef(post.getAuthor()));
46+
return of(todayRecipe);
47+
}
48+
);
49+
}
50+
51+
@Scheduled(cron = "00 00 * * * *")
52+
public void updateTodayRecipeAtMidNight() {
53+
Optional<TodayRecipe> todayRecipe = getTodayRecipe();
54+
todayRecipe.ifPresent(todayRecipeRepository::save);
55+
log.info("Update the today recipe - {}", LocalDate.now());
56+
}
57+
}

src/main/java/com/otchi/application/impl/ChefProfileServiceImpl.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.otchi.application.impl;
22

33
import com.otchi.application.*;
4+
import com.otchi.application.ChefProfileService;
5+
import com.otchi.application.UserService;
6+
import com.otchi.domain.kitchen.Chef;
47
import com.otchi.domain.users.exceptions.UserNotFoundException;
58
import com.otchi.domain.users.models.User;
69
import org.springframework.beans.factory.annotation.Autowired;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.otchi.domain.kitchen;
2+
3+
import com.otchi.domain.users.models.User;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.Table;
9+
import javax.validation.constraints.NotNull;
10+
import java.util.Objects;
11+
12+
@Entity
13+
@Table(name = "USERS")
14+
public class Chef {
15+
16+
@Id
17+
@Column(name = "ID")
18+
private Long id;
19+
20+
@NotNull
21+
@Column(name = "FIRST_NAME", length = 50)
22+
private String firstName;
23+
24+
@NotNull
25+
@Column(name = "LAST_NAME", length = 50)
26+
private String lastName;
27+
28+
@Column(name = "PICTURE")
29+
private String picture;
30+
31+
32+
private Chef() {
33+
}
34+
35+
public Chef(User chef) {
36+
this.id = chef.getId();
37+
this.firstName = chef.getFirstName();
38+
this.lastName = chef.getLastName();
39+
this.picture = chef.picture();
40+
}
41+
42+
43+
public Long getId() {
44+
return id;
45+
}
46+
47+
public String getFirstName() {
48+
return firstName;
49+
}
50+
51+
public String getLastName() {
52+
return lastName;
53+
}
54+
55+
public String getPicture() {
56+
return picture;
57+
}
58+
59+
public Chef(Long id, String firstName, String lastName, String picture) {
60+
this.id = id;
61+
this.firstName = firstName;
62+
this.lastName = lastName;
63+
this.picture = picture;
64+
}
65+
66+
@Override
67+
public boolean equals(Object o) {
68+
if (this == o) return true;
69+
if (o == null || getClass() != o.getClass()) return false;
70+
Chef chef = (Chef) o;
71+
return Objects.equals(id, chef.id) &&
72+
Objects.equals(firstName, chef.firstName) &&
73+
Objects.equals(lastName, chef.lastName) &&
74+
Objects.equals(picture, chef.picture);
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
return Objects.hash(id, firstName, lastName, picture);
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.otchi.domain.kitchen;
2+
3+
import javax.persistence.*;
4+
import java.time.LocalDate;
5+
import java.util.Objects;
6+
7+
@Entity
8+
@Table(name = "TODAY_RECIPE")
9+
public class TodayRecipe {
10+
11+
@Id
12+
@Column(name = "ID")
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
private Long id;
15+
16+
@Column(name = "POST_ID")
17+
private Long postId;
18+
19+
@Column(name = "IMAGE")
20+
private String image;
21+
22+
@Column(name = "TITLE")
23+
private String title;
24+
25+
26+
@Column(name = "DATE")
27+
private LocalDate date;
28+
29+
@ManyToOne
30+
@JoinColumn(name = "CHEF_ID")
31+
private Chef chef;
32+
33+
34+
public TodayRecipe(Long postId, String image, String title, Chef chef) {
35+
this.postId = postId;
36+
this.image = image;
37+
this.title = title;
38+
this.chef = chef;
39+
}
40+
41+
private TodayRecipe() {
42+
}
43+
44+
public Long getPostId() {
45+
return postId;
46+
}
47+
48+
public String getImage() {
49+
return image;
50+
}
51+
52+
public String getTitle() {
53+
return title;
54+
}
55+
56+
public Chef getChef() {
57+
return chef;
58+
}
59+
60+
@Override
61+
public boolean equals(Object o) {
62+
if (this == o) return true;
63+
if (o == null || getClass() != o.getClass()) return false;
64+
TodayRecipe that = (TodayRecipe) o;
65+
return Objects.equals(id, that.id) &&
66+
Objects.equals(postId, that.postId) &&
67+
Objects.equals(image, that.image) &&
68+
Objects.equals(title, that.title) &&
69+
Objects.equals(date, that.date) &&
70+
Objects.equals(chef, that.chef);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(id, postId, image, title, date, chef);
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.otchi.domain.kitchen;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
import java.util.Optional;
7+
8+
@Repository
9+
public interface TodayRecipeRepository extends CrudRepository<TodayRecipe, Long> {
10+
Optional<TodayRecipe> findFirstByOrderByDateDesc();
11+
}

0 commit comments

Comments
 (0)