Skip to content

Commit ae91a67

Browse files
committed
chapter exercises 1
1 parent 8219dea commit ae91a67

9 files changed

Lines changed: 224 additions & 35 deletions

File tree

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
package org.launchcode.codingevents.controllers;
22

3+
import org.launchcode.codingevents.models.Event;
34
import org.springframework.stereotype.Controller;
45
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PostMapping;
57
import org.springframework.web.bind.annotation.RequestMapping;
68
import org.springframework.ui.Model;
9+
import org.springframework.web.bind.annotation.RequestParam;
710

811
import java.util.ArrayList;
912
import java.util.List;
1013

11-
/**
12-
* Created by Chris Bay
13-
*/
14+
1415
@Controller
1516
@RequestMapping("events")
1617
public class EventController {
18+
private static List<Event> events = new ArrayList<>();
1719

1820
@GetMapping
1921
public String displayAllEvents(Model model) {
20-
List<String> events = new ArrayList<>();
21-
events.add("Code With Pride");
22-
events.add("Strange Loop");
23-
events.add("Apple WWDC");
24-
events.add("SpringOne Platform");
25-
model.addAttribute("events", events);
22+
model.addAttribute("events", events);
23+
// List<String> events = new ArrayList<>();
24+
// events.add("Code With Pride");
25+
// events.add("Strange Loop");
26+
// events.add("Apple WWDC");
27+
// events.add("SpringOne Platform");
28+
// model.addAttribute("events", events);
2629
return "events/index";
2730
}
2831

32+
@GetMapping("create")
33+
public String renderCreateEventForm() {
34+
35+
return "events/create";
36+
}
37+
38+
@PostMapping("create")
39+
public String createEvent(@RequestParam String eventName, @RequestParam String eventDescription) {
40+
events.add(new Event(eventName, eventDescription));
41+
return "redirect:/events";
42+
}
43+
2944
}

src/main/java/org/launchcode/codingevents/controllers/HomeController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import org.springframework.stereotype.Controller;
44
import org.springframework.web.bind.annotation.GetMapping;
55

6-
/**
7-
* Created by Chris Bay
8-
*/
6+
97
@Controller
108
public class HomeController {
119

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.launchcode.codingevents.models;
2+
3+
import java.util.Objects;
4+
5+
public class Event {
6+
7+
private int id;
8+
private static int nextID = 1;
9+
private String name;
10+
private String description;
11+
12+
public Event(String name, String description) {
13+
this.name = name;
14+
this.description = description;
15+
this.id = nextID;
16+
nextID++;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getDescription() {
28+
return description;
29+
}
30+
31+
public void setDescription(String description) {
32+
this.description = description;
33+
}
34+
35+
public int getId() {
36+
return id;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return name;
42+
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o) return true;
47+
if (o == null || getClass() != o.getClass()) return false;
48+
Event event = (Event) o;
49+
return id == event.id;
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return Objects.hash(id);
55+
}
56+
}

src/main/resources/static/script.js

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
font-size: 18px;
3+
color:purple;
4+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!--<html lang="en" xmlns:th="https://www.thymeleaf.org/">-->
2+
<!--<head>-->
3+
<!-- <meta charset="UTF-8"/>-->
4+
<!-- <title>Create</title>-->
5+
<!--</head>-->
6+
<!--<body>-->
7+
<!--<h1>Create Event</h1>-->
8+
9+
<!--<nav th:replace="fragments : : navigation"></nav>-->
10+
11+
<!--<form method="post" action="/events/create" >-->
12+
<!-- <input type="text" name="eventName">-->
13+
<!-- <input type="submit" value="Create Event">-->
14+
<!--</form>-->
15+
16+
<!--</body>-->
17+
<!--</html>-->
18+
<!DOCTYPE html>
19+
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
20+
<head th:replace="fragments :: head"></head>
21+
<body class="container" >
22+
23+
<header th:replace="fragments :: header"></header>
24+
25+
<form method="post">
26+
<div class="form-group">
27+
<label>Name
28+
<input type="text" name="eventDescription" class="form-control">
29+
</label>
30+
</div>
31+
<div class="form-group">
32+
<label>Description
33+
<input type="text" name="eventName" class="form-control">
34+
</label>
35+
</div>
36+
<div class="form-group">
37+
<input type="submit" value="Create" class="btn btn-success">
38+
</div>
39+
</form>
40+
41+
</body>
42+
</html>
Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
1+
<!--<!DOCTYPE html>-->
2+
<!--<html lang="en" xmlns:th="http://www.thymeleaf.org/">-->
3+
<!--<head>-->
4+
<!-- <meta charset="UTF-8"/>-->
5+
<!-- <title>Coding Events</title>-->
6+
<!--</head>-->
7+
<!--<body>-->
8+
9+
<!--<h1>All Events</h1>-->
10+
11+
<!--<nav th:replace="fragments : : navigation"> </nav>-->
12+
13+
<!--<p th:unless="${events} and ${events.size()}">No Events!</p>-->
14+
15+
<!--<ul>-->
16+
<!-- <th:block th:each="event : ${events}">-->
17+
<!-- <li th:text="${event}"></li>-->
18+
<!-- </th:block>-->
19+
<!--</ul>-->
20+
21+
<!--</body>-->
22+
<!--</html>-->
123
<!DOCTYPE html>
224
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3-
<head>
4-
<meta charset="UTF-8"/>
5-
<title>Coding Events</title>
6-
</head>
7-
<body>
8-
9-
<h1>All Events</h1>
10-
11-
<ul>
12-
<th:block th:each="event : ${events}">
13-
<li th:text="${event}"></li>
14-
</th:block>
15-
</ul>
25+
<head th:replace="fragments :: head"></head>
26+
<body class="container">
27+
28+
<header th:replace="fragments :: header"></header>
29+
30+
<table class="table table-striped">
31+
<thead>
32+
<tr>
33+
<th>ID</th>
34+
<th>Name</th>
35+
<th>Description</th>
36+
</tr>
37+
</thead>
38+
<tr th:each="event : ${events}">
39+
<td th:text="${event.id}"></td>
40+
<td th:text="${event.name}"></td>
41+
<td th:text="${event.description}"></td>
42+
</tr>
43+
</table>
1644

1745
</body>
1846
</html>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!--<!DOCTYPE html>-->
2+
<!--<html lang="en" xmlns:th="http://www.thymeleaf.org/">-->
3+
<!--<head th:fragment="head">-->
4+
<!-- <meta charset="UTF-8"/>-->
5+
<!-- <title>Coding Events</title>-->
6+
7+
<!-- <script th:src="@{/script.js}"></script>-->
8+
<!-- <link th:href="@{/style.css}" rel="stylesheet">-->
9+
<!--</head>-->
10+
<!--<body>-->
11+
12+
<!--<nav th:fragment="navigation">-->
13+
<!-- <ul>-->
14+
<!-- <li><a href="/events">All Events</a></li>-->
15+
<!-- <li><a href="/events/create">Create Event</a></li>-->
16+
<!-- </ul>-->
17+
<!--</nav>-->
18+
19+
<!--</body>-->
20+
<!--</html>-->
21+
<!DOCTYPE html>
22+
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
23+
<head th:fragment="head">
24+
<meta charset="UTF-8"/>
25+
<title>Coding Events</title>
26+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
27+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
28+
</head>
29+
<body>
30+
31+
<nav th:fragment="header">
32+
<h1 th:text="${title}">Coding Events</h1>
33+
<ul class="nav">
34+
<li class="nav-item"><a class="nav-link" href="/events">All Events</a></li>
35+
<li class="nav-item"><a class="nav-link" href="/events/create">Create Event</a></li>
36+
</ul>
37+
</nav>
38+
39+
</body>
40+
</html>
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
<!--<!DOCTYPE html>-->
2+
<!--<html lang="en" xmlns:th="http://www.thymeleaf.org/">-->
3+
<!--<head th:replace="fragments :: head">-->
4+
5+
<!--</head>-->
6+
<!--<body>-->
7+
8+
<!--<h1>Coding Events</h1>-->
9+
10+
<!--<nav th:replace="fragments : : navigation"></nav>-->
11+
12+
<!--</body>-->
13+
<!--</html>-->
114
<!DOCTYPE html>
215
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
3-
<head>
4-
<meta charset="UTF-8"/>
5-
<title>Coding Events</title>
6-
</head>
7-
<body>
16+
<head th:replace="fragments :: head"></head>
17+
<body class="container" >
818

919
<h1>Coding Events</h1>
1020

11-
<nav>
12-
<ul>
13-
<li><a href="/events">All Events</a></li>
14-
</ul>
15-
</nav>
21+
<header th:replace="fragments :: header"></header>
1622

1723
</body>
1824
</html>

0 commit comments

Comments
 (0)