Skip to content

Commit

Permalink
save and update finally freaking working, thanks Phil!
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldpearson committed Jan 10, 2019
1 parent 6c8a37c commit 8ad2885
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public ProducerController(ProducerRepository producerRepository) {
this.producerRepository = producerRepository;
}


@RequestMapping("/list")
public String producer(Model model) {
model.addAttribute("producers", producerRepository.findAll());
Expand All @@ -43,24 +42,24 @@ public String processCreateForm(@Valid Producer producer, BindingResult result)
return CREATE_UPDATE_PRODUCER_FORM;
} else {
producerRepository.save(producer);
return "forward:/producers/list";
return "redirect:/producers/list";
}
}

@GetMapping("/{producerId}/edit")
public String initUpdateProducerForm(@PathVariable("producerId") Long producerId, Model model) {
model.addAttribute(producerRepository.findById(producerId));
public String initUpdateProducerForm(@PathVariable Long producerId, Model model) {
model.addAttribute("producer", producerRepository.findById(producerId));
return CREATE_UPDATE_PRODUCER_FORM;
}

@PostMapping("/{producerId}/edit")
public String processUpdateProducerForm(@Valid Producer producer, BindingResult result, @PathVariable("producerId") Long producerId) {
public String processUpdateProducerForm(@Valid Producer producer, BindingResult result, @PathVariable Long producerId) {
if(result.hasErrors()) {
return CREATE_UPDATE_PRODUCER_FORM;
} else {
producer.setId(producerId);
Producer savedProducer = producerRepository.save(producer);
return "redirect:/producers/" + savedProducer.getId();
producerRepository.save(producer);
return "redirect:/producers/list";
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/cellar/wine/controllers/WineController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String wine(Model model) {
}

@GetMapping("/new")
public String initCreationForm(Wine wine, Model model) {
public String initCreationForm(Model model) {
model.addAttribute("wine", Wine.builder().build());
return CREATE_UPDATE_WINE_FORM;
}
Expand All @@ -42,7 +42,7 @@ public String processCreationForm(@Valid Wine wine, BindingResult result) {
return CREATE_UPDATE_WINE_FORM;
} else {
wineRepository.save(wine);
return "forward:/wines/list";
return "redirect:/wines/list";
}
}

Expand All @@ -53,12 +53,12 @@ public String initUpdateForm(@PathVariable Long wineId, Model model) {
}

@PostMapping("/{wineId}/edit")
public String processUpdateForm(@Valid Wine wine, @PathVariable Long wineId, BindingResult result, Model model) {
public String processUpdateForm(@Valid Wine wine, BindingResult result, @PathVariable Long wineId) {
if (result.hasErrors()) {
model.addAttribute("wine", wine);
return CREATE_UPDATE_WINE_FORM;
} else {
model.addAttribute("wine", wineRepository.findById(wineId));
wine.setId(wineId);
wineRepository.save(wine);
return "redirect:/wines/list";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ spring.h2.console.enabled=true
#spring.datasource.username=wine_user
#spring.datasource.password=password

#server.port=7070
server.port=7070
7 changes: 4 additions & 3 deletions src/main/resources/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@
background-size: cover;
}

body {
overflow: hidden;
}
/*THIS DOES NOT WORK!!! It removes the ability to scroll*/
/*body {*/
/*overflow: hidden;*/
/*}*/

.nav-item {
text-align: center;
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

<nav th:insert="~{fragments/layout :: navbar}"></nav>

<!--<div class="jumbotron jumbotron-fluid">-->
<!--<div class="container text-center">-->
<!--<h1>Welcome to the Wine Cellar</h1>-->
<!--</div>-->
<!--</div>-->

<div class="container-fluid">
<div class="row">
<img class='img-fluid w-100' style="background-size: 100%; background-repeat: no-repeat" src="#" th:src="@{/images/wine.jpg}" alt="wine bottle"/>
<div class="jumbotron jumbotron-fluid">
<div class="container text-center">
<h1>Welcome</h1>
</div>
</div>

<!--<div class="container-fluid">-->
<!--<div class="row">-->
<!--<img class='img-fluid w-100' style="background-size: 100%; background-repeat: no-repeat" src="#" th:src="@{/images/wine.jpg}" alt="wine bottle"/>-->
<!--</div>-->
<!--</div>-->

<footer th:insert="~{fragments/layout :: footer}"></footer>

</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@

<div class="container">
<h2>Add Producer:</h2>
<form th:modelAttribute="producer" method="post" th:object="${producer}">
<form method="post" th:object="${producer}">

<div class="form-group has-feedback">
<label for="name">Name:</label>
<input type="text" class="form-control" th:field="*{name}" id="name" placeholder="Enter name"
name="name">
</div>

<div class="form-group has-feedback">
<label for="appellation">Appellation:</label>
<input type="text" class="form-control" id="appellation" th:field="*{appellation}"
placeholder="Enter appellation" name="appellation">
</div>

<div class="form-group has-feedback">
<label for="country">Country:</label>
<input type="text" class="form-control" id="country" th:field="*{country}"
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/templates/producers/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
<th>Name</th>
<th>Country</th>
<th>Appellation</th>
<th></th>
</tr>
<tr th:each="producer : ${producers}">
<td><a href="#" th:href="@{'/producers/' + ${producer.id}}" th:text="${producer.name}">Name</a></td>
<td th:text="${producer.name}">Producer Name</td>
<!--<td><a href="#" th:href="@{'/producers/' + ${producer.id}}" th:text="${producer.name}">Name</a></td>-->
<td th:text="${producer.country}">Country</td>
<td th:text="${producer.appellation}">Appellation</td>
<td><a th:href="@{/producers/{id}/edit(id=${producer.id})}" class="btn btn-default">Update</a></td>
</tr>
</thead>
</table>
<button type="submit" class="btn btn-default" onclick="window.location.href='/producers/new'; return false;">Add Producer</button>
<a th:href="@{/producers/new}" class="btn btn-default">Add Producer</a>
</div>

<footer th:include="~{fragments/layout :: footer}"></footer>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/wines/createOrUpdateWine.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<div class="container">
<h2>Add Wine:</h2>
<form th:modelAttribute="wine" method="post" th:object="${wine}">
<form method="post" th:object="${wine}">
<div class="form-group has-feedback">
<label for="name">Name:</label>
<input type="text" class="form-control" th:field="*{name}" id="name" placeholder="Enter name"
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/templates/wines/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
<th>Appellation</th>
<th>Varietal</th>
<th>Country</th>
<th></th>
</tr>
<tr th:each="wine : ${wines}">
<td th:text="${wine.name}">Name</td>
<td th:text="${wine.vintage}">Vintage</td>
<td th:text="${wine.appellation}">Appellation</td>
<td th:text="${wine.varietal}">Varietal</td>
<td th:text="${wine.country}">Country</td>
<td><a th:href="@{/wines/{id}/edit(id=${wine.id})}" class="btn btn-default">Update</a></td>
</tr>
</thead>
</table>

<button type="submit" class="save btn" onclick="window.location.href='/wines/new'; return false;">Add Wine</button>
<a th:href="@{/wines/new}" class="btn btn-default">Add Wine</a>

</div>

Expand Down

0 comments on commit 8ad2885

Please sign in to comment.