From b4bf56392b20329954d5b0fbf006612eb4c93e82 Mon Sep 17 00:00:00 2001 From: Chris Bay Date: Thu, 23 Mar 2017 17:47:32 -0500 Subject: [PATCH 1/5] Finish exercise --- .../controllers/CheeseController.java | 26 ++++++++++++++++--- src/main/resources/templates/cheese/edit.html | 23 ++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/templates/cheese/edit.html diff --git a/src/main/java/org/launchcode/controllers/CheeseController.java b/src/main/java/org/launchcode/controllers/CheeseController.java index d6c5d45..7da5321 100644 --- a/src/main/java/org/launchcode/controllers/CheeseController.java +++ b/src/main/java/org/launchcode/controllers/CheeseController.java @@ -4,11 +4,9 @@ import org.launchcode.models.CheeseData; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; +import javax.websocket.server.PathParam; import java.util.ArrayList; /** @@ -57,4 +55,24 @@ public String processRemoveCheeseForm(@RequestParam int[] cheeseIds) { return "redirect:"; } + + @RequestMapping(value = "edit/{cheeseId}", method = RequestMethod.GET) + public String displayEditForm(Model model, @PathVariable int cheeseId) { + + Cheese theCheese = CheeseData.getById(cheeseId); + model.addAttribute("cheese", theCheese); + return "cheese/edit"; + } + + @RequestMapping(value = "edit/{cheeseId}", method = RequestMethod.POST) + public String processEditForm(@PathVariable int cheeseId, + String name, String description) { + + Cheese theCheese = CheeseData.getById(cheeseId); + theCheese.setName(name); + theCheese.setDescription(description); + + return "redirect:"; + } + } diff --git a/src/main/resources/templates/cheese/edit.html b/src/main/resources/templates/cheese/edit.html new file mode 100644 index 0000000..26ba964 --- /dev/null +++ b/src/main/resources/templates/cheese/edit.html @@ -0,0 +1,23 @@ + + + + +

Default Title

+ + + +
+
+ + +
+
+ + +
+ + +
+ + + \ No newline at end of file From f54657e261d95c52550b226e8df1aa51aa675f57 Mon Sep 17 00:00:00 2001 From: Chris Bay Date: Sat, 25 Mar 2017 23:34:40 -0500 Subject: [PATCH 2/5] Finish studio --- .../controllers/UserController.java | 38 ++++++++++++++++ src/main/java/org/launchcode/models/User.java | 43 +++++++++++++++++++ src/main/resources/templates/user/add.html | 35 +++++++++++++++ src/main/resources/templates/user/index.html | 12 ++++++ 4 files changed, 128 insertions(+) create mode 100644 src/main/java/org/launchcode/controllers/UserController.java create mode 100644 src/main/java/org/launchcode/models/User.java create mode 100644 src/main/resources/templates/user/add.html create mode 100644 src/main/resources/templates/user/index.html diff --git a/src/main/java/org/launchcode/controllers/UserController.java b/src/main/java/org/launchcode/controllers/UserController.java new file mode 100644 index 0000000..fb5e442 --- /dev/null +++ b/src/main/java/org/launchcode/controllers/UserController.java @@ -0,0 +1,38 @@ +package org.launchcode.controllers; + +import org.launchcode.models.User; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Created by LaunchCode + */ +@Controller +@RequestMapping("user") +public class UserController { + + @RequestMapping(value = "add", method = RequestMethod.GET) + public String add(Model model) { + return "user/add"; + } + + @RequestMapping(value = "add", method = RequestMethod.POST) + public String add(Model model, User user, String verify) { + + if (user.getPassword() != null + && user.getPassword().equals(verify)) { + model.addAttribute("name", user.getUsername()); + return "user/index"; + } + + model.addAttribute("username", user.getUsername()); + model.addAttribute("email", user.getEmail()); + model.addAttribute("error", "Passwords do not match"); + return "user/add"; + + } + + +} diff --git a/src/main/java/org/launchcode/models/User.java b/src/main/java/org/launchcode/models/User.java new file mode 100644 index 0000000..1a960c3 --- /dev/null +++ b/src/main/java/org/launchcode/models/User.java @@ -0,0 +1,43 @@ +package org.launchcode.models; + +/** + * Created by LaunchCode + */ +public class User { + + private String username; + private String email; + private String password; + + public User(String username, String email, String password) { + this.username = username; + this.email = email; + this.password = password; + } + + public User() {} + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/resources/templates/user/add.html b/src/main/resources/templates/user/add.html new file mode 100644 index 0000000..48f7842 --- /dev/null +++ b/src/main/resources/templates/user/add.html @@ -0,0 +1,35 @@ + + + + + + + + +

Register

+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ + +
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/user/index.html b/src/main/resources/templates/user/index.html new file mode 100644 index 0000000..21c77d2 --- /dev/null +++ b/src/main/resources/templates/user/index.html @@ -0,0 +1,12 @@ + + + + + + + + +

+ + + \ No newline at end of file From 3040a021219dbce8f70c9ca8c51cb4d3bb7cf284 Mon Sep 17 00:00:00 2001 From: Chris Bay Date: Sun, 26 Mar 2017 09:58:16 -0500 Subject: [PATCH 3/5] Studio solution --- .../controllers/UserController.java | 25 ++++++++++---- src/main/java/org/launchcode/models/User.java | 12 +++++++ src/main/resources/templates/user/add.html | 34 +++++++++---------- src/main/resources/templates/user/index.html | 12 +++---- 4 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/launchcode/controllers/UserController.java b/src/main/java/org/launchcode/controllers/UserController.java index fb5e442..1eeb9b1 100644 --- a/src/main/java/org/launchcode/controllers/UserController.java +++ b/src/main/java/org/launchcode/controllers/UserController.java @@ -3,9 +3,13 @@ import org.launchcode.models.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.Errors; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import javax.validation.Valid; + /** * Created by LaunchCode */ @@ -15,21 +19,28 @@ public class UserController { @RequestMapping(value = "add", method = RequestMethod.GET) public String add(Model model) { + model.addAttribute(new User()); + model.addAttribute("title", "Register"); return "user/add"; } @RequestMapping(value = "add", method = RequestMethod.POST) - public String add(Model model, User user, String verify) { + public String add(Model model, @ModelAttribute @Valid User user, + Errors errors, String verify) { + + model.addAttribute(user); + boolean passwordsMatch = true; + if (user.getPassword() == null || verify == null + || !user.getPassword().equals(verify)) { + passwordsMatch = false; + user.setPassword(""); + model.addAttribute("verifyError", "Passwords must match"); + } - if (user.getPassword() != null - && user.getPassword().equals(verify)) { - model.addAttribute("name", user.getUsername()); + if (!errors.hasErrors() && passwordsMatch) { return "user/index"; } - model.addAttribute("username", user.getUsername()); - model.addAttribute("email", user.getEmail()); - model.addAttribute("error", "Passwords do not match"); return "user/add"; } diff --git a/src/main/java/org/launchcode/models/User.java b/src/main/java/org/launchcode/models/User.java index 1a960c3..5a77e28 100644 --- a/src/main/java/org/launchcode/models/User.java +++ b/src/main/java/org/launchcode/models/User.java @@ -1,12 +1,24 @@ package org.launchcode.models; +import org.hibernate.validator.constraints.Email; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + /** * Created by LaunchCode */ public class User { + @NotNull + @Size(min= 5, max= 15) private String username; + + @Email(message = "Invalid email address") private String email; + + @NotNull + @Size(min=5, message = "Password must be at least 5 characters long") private String password; public User(String username, String email, String password) { diff --git a/src/main/resources/templates/user/add.html b/src/main/resources/templates/user/add.html index 48f7842..10c4d88 100644 --- a/src/main/resources/templates/user/add.html +++ b/src/main/resources/templates/user/add.html @@ -1,33 +1,33 @@ - - - - - + + +

Default Title

-

Register

+ -
+
- - + + +
- - + + +
- - + + +
- - + + +
-
-
diff --git a/src/main/resources/templates/user/index.html b/src/main/resources/templates/user/index.html index 21c77d2..8169100 100644 --- a/src/main/resources/templates/user/index.html +++ b/src/main/resources/templates/user/index.html @@ -1,12 +1,12 @@ - - - - - + + +

Default Title

-

+ + +

\ No newline at end of file From 833d4e9515531a3252f890360313ccb7b6dd7a22 Mon Sep 17 00:00:00 2001 From: Chris Bay Date: Sun, 26 Mar 2017 09:59:00 -0500 Subject: [PATCH 4/5] Remove misplaced div --- src/main/resources/templates/user/add.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/resources/templates/user/add.html b/src/main/resources/templates/user/add.html index 48f7842..3ffb90a 100644 --- a/src/main/resources/templates/user/add.html +++ b/src/main/resources/templates/user/add.html @@ -26,8 +26,6 @@

Register

-
- From c7ff145c57c3f3ca4dbcfbaaf437f6b9f54328ff Mon Sep 17 00:00:00 2001 From: Chris Bay Date: Sun, 26 Mar 2017 10:42:13 -0500 Subject: [PATCH 5/5] Finish bonus mission --- .../controllers/UserController.java | 11 ++-------- src/main/java/org/launchcode/models/User.java | 20 +++++++++++++++++++ src/main/resources/templates/user/add.html | 6 +++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/launchcode/controllers/UserController.java b/src/main/java/org/launchcode/controllers/UserController.java index 1eeb9b1..ca6733f 100644 --- a/src/main/java/org/launchcode/controllers/UserController.java +++ b/src/main/java/org/launchcode/controllers/UserController.java @@ -26,18 +26,11 @@ public String add(Model model) { @RequestMapping(value = "add", method = RequestMethod.POST) public String add(Model model, @ModelAttribute @Valid User user, - Errors errors, String verify) { + Errors errors) { model.addAttribute(user); - boolean passwordsMatch = true; - if (user.getPassword() == null || verify == null - || !user.getPassword().equals(verify)) { - passwordsMatch = false; - user.setPassword(""); - model.addAttribute("verifyError", "Passwords must match"); - } - if (!errors.hasErrors() && passwordsMatch) { + if (!errors.hasErrors()) { return "user/index"; } diff --git a/src/main/java/org/launchcode/models/User.java b/src/main/java/org/launchcode/models/User.java index 5a77e28..4669790 100644 --- a/src/main/java/org/launchcode/models/User.java +++ b/src/main/java/org/launchcode/models/User.java @@ -21,6 +21,9 @@ public class User { @Size(min=5, message = "Password must be at least 5 characters long") private String password; + @NotNull(message = "Passwords do not match") + private String verifyPassword; + public User(String username, String email, String password) { this.username = username; this.email = email; @@ -51,5 +54,22 @@ public String getPassword() { public void setPassword(String password) { this.password = password; + checkPassword(); + } + + public String getVerifyPassword() { + return verifyPassword; + } + + public void setVerifyPassword(String verifyPassword) { + this.verifyPassword = verifyPassword; + checkPassword(); + } + + private void checkPassword() { + if (password != null && verifyPassword != null + && !password.equals(verifyPassword)) { + verifyPassword = null; + } } } diff --git a/src/main/resources/templates/user/add.html b/src/main/resources/templates/user/add.html index 10c4d88..ec03fb4 100644 --- a/src/main/resources/templates/user/add.html +++ b/src/main/resources/templates/user/add.html @@ -23,9 +23,9 @@

Default Title

- - -
+ + +