Skip to content

Commit

Permalink
The creation date should be assigned by the repository when the entit…
Browse files Browse the repository at this point in the history
…y is saved
  • Loading branch information
kitdim committed Feb 6, 2024
1 parent 122b197 commit 6f9052a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 1 addition & 3 deletions app/src/main/java/hexlet/code/controller/UrlController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.net.URL;
import java.util.Collections;
import java.sql.SQLException;
import java.sql.Timestamp;

@Slf4j
public class UrlController {
Expand Down Expand Up @@ -65,8 +64,7 @@ public static void create(Context ctx) throws SQLException {
ctx.redirect(NamedRoutes.urlsPath());
} else {
log.info("success");
Timestamp createdAt = new Timestamp(System.currentTimeMillis());
Url url = Url.builder().name(normalizedUrl).createdAt(createdAt).build();
Url url = Url.builder().name(normalizedUrl).build();
UrlsRepository.save(url);
ctx.sessionAttribute("flash", SUCCESSFULLY);
ctx.sessionAttribute("flash-type", "success");
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/hexlet/code/repository/UrlsRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
public class UrlsRepository extends BaseRepository {
public static void save(Url url) throws SQLException {
String sql = "INSERT INTO urls (name, created_at) VALUES (?, ?)";
Timestamp createdAt = new Timestamp(System.currentTimeMillis());
url.setCreatedAt(createdAt);
try (var conn = dataSource.getConnection();
var stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, url.getName());
Expand Down

0 comments on commit 6f9052a

Please sign in to comment.