From 6f9052ac27b18c67edaad4d62c322d072f1824dd Mon Sep 17 00:00:00 2001 From: kitdim Date: Tue, 6 Feb 2024 08:26:16 +0300 Subject: [PATCH] The creation date should be assigned by the repository when the entity is saved --- app/src/main/java/hexlet/code/controller/UrlController.java | 4 +--- app/src/main/java/hexlet/code/repository/UrlsRepository.java | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/hexlet/code/controller/UrlController.java b/app/src/main/java/hexlet/code/controller/UrlController.java index 361bc72..56f85e8 100644 --- a/app/src/main/java/hexlet/code/controller/UrlController.java +++ b/app/src/main/java/hexlet/code/controller/UrlController.java @@ -13,7 +13,6 @@ import java.net.URL; import java.util.Collections; import java.sql.SQLException; -import java.sql.Timestamp; @Slf4j public class UrlController { @@ -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"); diff --git a/app/src/main/java/hexlet/code/repository/UrlsRepository.java b/app/src/main/java/hexlet/code/repository/UrlsRepository.java index 68b2086..4cf7fe2 100644 --- a/app/src/main/java/hexlet/code/repository/UrlsRepository.java +++ b/app/src/main/java/hexlet/code/repository/UrlsRepository.java @@ -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());