44import hexlet .code .dto .urls .UrlPage ;
55import hexlet .code .dto .urls .UrlsPage ;
66import hexlet .code .model .Url ;
7+ import hexlet .code .model .UrlCheck ;
8+ import hexlet .code .repository .UrlCheckRepository ;
79import hexlet .code .repository .UrlRepository ;
810import hexlet .code .util .NamedRoutes ;
911
12+ import static hexlet .code .util .AppSettings .FLASH ;
13+ import static hexlet .code .util .AppSettings .FLASH_DANGER ;
14+ import static hexlet .code .util .AppSettings .FLASH_INFO ;
15+ import static hexlet .code .util .AppSettings .FLASH_SUCCESS ;
16+ import static hexlet .code .util .AppSettings .FLASH_TYPE ;
17+ import static hexlet .code .util .AppSettings .FLASH_WARNING ;
18+ import static hexlet .code .util .AppSettings .PAGE_ADDED ;
19+ import static hexlet .code .util .AppSettings .PAGE_EXIST ;
20+ import static hexlet .code .util .AppSettings .URL_BAD ;
21+ import static hexlet .code .util .AppSettings .URL_EMPTY ;
22+
1023import io .javalin .http .Context ;
1124import io .javalin .http .NotFoundResponse ;
1225
2033import java .sql .SQLException ;
2134import java .time .LocalDateTime ;
2235import java .util .List ;
36+ import java .util .Map ;
2337
2438import lombok .extern .slf4j .Slf4j ;
2539import org .apache .http .client .utils .URIBuilder ;
2640
2741@ Slf4j
28- public class UrlController {
42+ public final class UrlController {
43+ private UrlController () {
44+ // Sonar warning
45+ // Prevent instantiation
46+ }
47+
2948 public static void build (Context ctx ) {
3049 BasePage page = new BasePage ();
31- page .setFlash (ctx .consumeSessionAttribute ("flash" ));
32- page .setFlashType (ctx .consumeSessionAttribute ("flash-type" ));
50+ page .setFlash (ctx .consumeSessionAttribute (FLASH ));
51+ page .setFlashType (ctx .consumeSessionAttribute (FLASH_TYPE ));
3352 ctx .render ("urls/build.jte" , model ("page" , page ));
3453 }
3554
3655 public static void create (Context ctx ) throws SQLException , URISyntaxException , MalformedURLException {
3756 String name = ctx .formParamAsClass ("url" , String .class ).get ();
38- URL unifmResourceId = null ;
57+ URL unifmResourceId ;
3958 try {
4059 unifmResourceId = new URI (name ).toURL ();
4160 } catch (Exception e ) {
42- ctx .sessionAttribute ("flash" , "Некорректный URL" );
43- ctx .sessionAttribute ("flash-type" , "danger" );
61+ ctx .sessionAttribute (FLASH , URL_BAD );
62+ ctx .sessionAttribute (FLASH_TYPE , FLASH_DANGER );
4463 ctx .redirect (NamedRoutes .buildPath ());
4564 return ;
4665 }
4766
48- if (name == null || name .isEmpty ()) {
49- ctx .sessionAttribute ("flash" , "Поле URL не должно быть пустым" );
50- ctx .sessionAttribute ("flash-type" , "warning" );
67+ if (name .isEmpty ()) {
68+ ctx .sessionAttribute (FLASH , URL_EMPTY );
69+ ctx .sessionAttribute (FLASH_TYPE , FLASH_WARNING );
5170 ctx .redirect (NamedRoutes .buildPath ());
5271 return ;
5372 }
@@ -62,32 +81,38 @@ public static void create(Context ctx) throws SQLException, URISyntaxException,
6281 Url newUrl = new Url (String .valueOf (url ), LocalDateTime .now ());
6382 UrlRepository .save (newUrl );
6483 } else {
65- ctx .sessionAttribute ("flash" , "Страница уже существует" );
66- ctx .sessionAttribute ("flash-type" , "info" );
84+ ctx .sessionAttribute (FLASH , PAGE_EXIST );
85+ ctx .sessionAttribute (FLASH_TYPE , FLASH_INFO );
6786 ctx .redirect (NamedRoutes .urlsPath ());
6887 return ;
6988 }
7089
71- ctx .sessionAttribute ("flash" , "Страница успешно добавлена" );
72- ctx .sessionAttribute ("flash-type" , "success" );
90+ ctx .sessionAttribute (FLASH , PAGE_ADDED );
91+ ctx .sessionAttribute (FLASH_TYPE , FLASH_SUCCESS );
7392 ctx .redirect (NamedRoutes .urlsPath ());
7493 }
7594
7695 public static void index (Context ctx ) throws SQLException {
7796 List <Url > urls = UrlRepository .getEntities ();
78- UrlsPage page = new UrlsPage (urls );
79- page .setFlash (ctx .consumeSessionAttribute ("flash" ));
80- page .setFlashType (ctx .consumeSessionAttribute ("flash-type" ));
97+ Map <Long , UrlCheck > latestChecks = UrlCheckRepository .getLastestChecks ();
98+
99+ UrlsPage page = new UrlsPage (urls , latestChecks );
100+ page .setFlash (ctx .consumeSessionAttribute (FLASH ));
101+ page .setFlashType (ctx .consumeSessionAttribute (FLASH_TYPE ));
81102 ctx .render ("urls/indexList.jte" , model ("page" , page ));
82103 }
83104
84105 public static void show (Context ctx ) throws SQLException {
85106 Long id = ctx .pathParamAsClass ("id" , Long .class ).get ();
86107 Url url = UrlRepository .find (id )
87108 .orElseThrow (() -> new NotFoundResponse ("Entity with id = " + id + " not found" ));
109+ List <UrlCheck > urlCheckList = UrlCheckRepository .findById (id );
110+ if (!urlCheckList .isEmpty ()) {
111+ url .setUrlCheckList (urlCheckList );
112+ }
88113 UrlPage page = new UrlPage (url );
89- page .setFlash (ctx .consumeSessionAttribute ("flash" ));
90- page .setFlashType (ctx .consumeSessionAttribute ("flash-type" ));
114+ page .setFlash (ctx .consumeSessionAttribute (FLASH ));
115+ page .setFlashType (ctx .consumeSessionAttribute (FLASH_TYPE ));
91116 ctx .render ("urls/show.jte" , model ("page" , page ));
92117 }
93118}
0 commit comments