Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

User signup cont bonus #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/main/java/org/launchcode/controllers/CheeseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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:";
}

}
42 changes: 42 additions & 0 deletions src/main/java/org/launchcode/controllers/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.launchcode.controllers;

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
*/
@Controller
@RequestMapping("user")
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, @ModelAttribute @Valid User user,
Errors errors) {

model.addAttribute(user);

if (!errors.hasErrors()) {
return "user/index";
}

return "user/add";

}


}
75 changes: 75 additions & 0 deletions src/main/java/org/launchcode/models/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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;

@NotNull(message = "Passwords do not match")
private String verifyPassword;

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;
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;
}
}
}
23 changes: 23 additions & 0 deletions src/main/resources/templates/cheese/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body class="container">
<h1 th:text="${title}">Default Title</h1>

<nav th:replace="fragments :: navigation"></nav>

<form method="post" style="max-width:600px;">
<div class="form-group">
<label for="name-field">Name</label>
<input class="form-control" id="name-field" type="text" name="name" th:value="${cheese.name}" />
</div>
<div class="form-group">
<label for="desc-field">Description</label>
<input class="form-control" id="desc-field" type="text" name="description" th:value="${cheese.description}" />
</div>
<input type="hidden" th:value="${cheese.cheeseId}" name="cheeseId" />
<input type="submit" value="Add Cheese" />
</form>

</body>
</html>
35 changes: 35 additions & 0 deletions src/main/resources/templates/user/add.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body class="container">
<h1 th:text="${title}">Default Title</h1>

<nav th:replace="fragments :: navigation"></nav>

<form method="post" th:object="${user}" style="width:600px;">
<div class="form-group">
<label th:for="username">Username</label>
<input class="form-control" th:field="*{username}" />
<div class="error" th:errors="*{username}"></div>
</div>
<div class="form-group">
<label th:for="email">Email</label>
<input class="form-control" th:field="*{email}" type="email" />
<div class="error" th:errors="*{email}"></div>
</div>
<div class="form-group">
<label th:for="password">Password</label>
<input class="form-control" th:field="*{password}" type="password" />
<div class="error" th:errors="*{password}"></div>
</div>
<div class="form-group">
<label th:for="verifyPassword">Verify</label>
<input class="form-control" th:field="*{verifyPassword}" type="password" />
<div class="error" th:errors="*{verifyPassword}"></div>
</div>

<input type="submit" value="Register" />
</form>

</body>
</html>
12 changes: 12 additions & 0 deletions src/main/resources/templates/user/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body class="container">
<h1 th:text="${title}">Default Title</h1>

<nav th:replace="fragments :: navigation"></nav>

<h1 th:text="${'Welcome ' + user.username + '!'}"></h1>

</body>
</html>