33import hexlet .code .dto .urls .UrlPage ;
44import hexlet .code .dto .urls .UrlsPage ;
55import hexlet .code .model .Url ;
6+ import hexlet .code .model .UrlCheck ;
7+ import hexlet .code .repository .UrlCheckRepository ;
68import hexlet .code .repository .UrlRepository ;
79import hexlet .code .util .NamedRoutes ;
810import io .javalin .http .Context ;
1214import java .net .URI ;
1315import java .net .URISyntaxException ;
1416import java .sql .SQLException ;
17+ import java .util .HashMap ;
1518
1619import static io .javalin .rendering .template .TemplateUtil .model ;
1720
1821public class UrlsController {
1922
2023 public static void index (Context ctx ) throws SQLException {
2124 var urls = UrlRepository .getEntities ();
22- var page = new UrlsPage (urls );
25+ var latestChecks = new HashMap <Long , UrlCheck >();
26+
27+ for (var check : UrlCheckRepository .getEntities ()) {
28+ latestChecks .put (check .getUrlId (), check );
29+ }
30+
31+ var page = new UrlsPage (urls , latestChecks );
2332 page .setFlash (ctx .consumeSessionAttribute ("flash" ));
2433 page .setFlashType (ctx .consumeSessionAttribute ("flash-type" ));
2534 ctx .render ("urls/index.jte" , model ("page" , page ));
@@ -28,7 +37,11 @@ public static void index(Context ctx) throws SQLException {
2837 public static void show (Context ctx ) throws SQLException {
2938 var id = ctx .pathParamAsClass ("id" , Long .class ).get ();
3039 var url = UrlRepository .find (id ).orElseThrow (NotFoundResponse ::new );
31- var page = new UrlPage (url );
40+ var checks = UrlCheckRepository .getEntities ().stream ()
41+ .filter (check -> check .getUrlId ().equals (id ))
42+ .toList ();
43+
44+ var page = new UrlPage (url , checks );
3245 ctx .render ("urls/show.jte" , model ("page" , page ));
3346 }
3447
0 commit comments