Skip to content

Commit

Permalink
Changed tests and add row
Browse files Browse the repository at this point in the history
  • Loading branch information
Timur Galeev committed Jan 14, 2021
1 parent ae43f42 commit fe00a76
Show file tree
Hide file tree
Showing 16 changed files with 281 additions and 319 deletions.
50 changes: 0 additions & 50 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,56 +95,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<inherited>false</inherited>
<groupId>com.google.code.maven-license-plugin</groupId>
<artifactId>maven-license-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<header>NOTICE</header>
<failIfMissing>true</failIfMissing>
<aggregate>true</aggregate>
<properties>
<owner>Timzu</owner>
<email>[email protected]</email>
</properties>
<excludes>
<exclude>mvnw.cmd</exclude>
<exclude>.mvn/**</exclude>
<exclude>.hg/**</exclude>
<exclude>.git/**</exclude>
<exclude>.gitignore/**</exclude>
<exclude>jenkins-jobs-configuration/**</exclude>
<exclude>LICENSE</exclude>
<exclude>NOTICE</exclude>
<exclude>README.md</exclude>
<exclude>**/Servers/**</exclude>
<exclude>**/src/test/resources/**</exclude>
<exclude>**/src/test/data/**</exclude>
<exclude>.git/**</exclude>
<exclude>**/.metadata/**</exclude>
<exclude>**/.idea/**</exclude>
<exclude>**/logs/**</exclude>
<exclude>**/js/**</exclude>
<exclude>**/css/**</exclude>
<exclude>**/src/main/resources/**</exclude>
<exclude>**/src/sql/**</exclude>
<exclude>**/nb-configuration.xml</exclude>
<exclude>**/nbactions.xml</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>check-headers</id>
<phase>verify</phase>
<goals>
<!--<goal>check</goal>-->
<goal>format</goal>
<!--<goal>remove</goal>-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
25 changes: 12 additions & 13 deletions src/main/java/com/timzu/spring/Application.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.timzu.simple;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
package com.timzu.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
306 changes: 153 additions & 153 deletions src/main/java/com/timzu/spring/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,153 +1,153 @@
package com.timzu.spring.controller;

import com.timzu.spring.entity.User;
import com.timzu.spring.service.UserService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

private final int ROW_PER_PAGE = 5;

@Autowired
private UserService userService;

@Value("${msg.title}")
private String title;

@GetMapping(value = {"/", "/index"})
public String index(Model model) {
model.addAttribute("title", title);
return "index";
}

@GetMapping(value = "/users")
public String getUsers(Model model,
@RequestParam(value = "page", defaultValue = "1") int pageNumber) {
List<User> users = userService.findAll(pageNumber, ROW_PER_PAGE);

long count = userService.count();
boolean hasPrev = pageNumber > 1;
boolean hasNext = (pageNumber * ROW_PER_PAGE) < count;
model.addAttribute("users", users);
model.addAttribute("hasPrev", hasPrev);
model.addAttribute("prev", pageNumber - 1);
model.addAttribute("hasNext", hasNext);
model.addAttribute("next", pageNumber + 1);
return "user-list";
}

@GetMapping(value = "/users/{userId}")
public String getUserById(Model model, @PathVariable long userId) {
User user = null;
try {
user = userService.findById(userId);
model.addAttribute("allowDelete", false);
} catch (Exception ex) {
model.addAttribute("errorMessage", ex.getMessage());
}
model.addAttribute("user", user);
return "user";
}

@GetMapping(value = {"/users/add"})
public String showAddUser(Model model) {
User user = new User();
model.addAttribute("add", true);
model.addAttribute("user", user);

return "user-edit";
}

@PostMapping(value = "/users/add")
public String addUser(Model model,
@ModelAttribute("user") User user) {
try {
User newUser = userService.save(user);
return "redirect:/users/" + String.valueOf(newUser.getId());
} catch (Exception ex) {
// log exception first,
// then show error
String errorMessage = ex.getMessage();
logger.error(errorMessage);
model.addAttribute("errorMessage", errorMessage);

model.addAttribute("add", true);
return "user-edit";
}
}

@GetMapping(value = {"/users/{userId}/edit"})
public String showEditUser(Model model, @PathVariable long userId) {
User user = null;
try {
user = userService.findById(userId);
} catch (Exception ex) {
model.addAttribute("errorMessage", ex.getMessage());
}
model.addAttribute("add", false);
model.addAttribute("user", user);
return "user-edit";
}

@PostMapping(value = {"/users/{userId}/edit"})
public String updateUser(Model model,
@PathVariable long userId,
@ModelAttribute("user") User user) {
try {
user.setId(userId);
userService.update(user);
return "redirect:/users/" + String.valueOf(user.getId());
} catch (Exception ex) {
// log exception first,
// then show error
String errorMessage = ex.getMessage();
logger.error(errorMessage);
model.addAttribute("errorMessage", errorMessage);

model.addAttribute("add", false);
return "user-edit";
}
}

@GetMapping(value = {"/users/{userId}/delete"})
public String showDeleteUser(
Model model, @PathVariable long userId) {
User user = null;
try {
user = userService.findById(userId);
} catch (Exception ex) {
model.addAttribute("errorMessage", ex.getMessage());
}
model.addAttribute("allowDelete", true);
model.addAttribute("user", user);
return "user";
}

@PostMapping(value = {"/users/{userId}/delete"})
public String deleteUserById(
Model model, @PathVariable long userId) {
try {
userService.deleteById(userId);
return "redirect:/users";
} catch (Exception ex) {
String errorMessage = ex.getMessage();
logger.error(errorMessage);
model.addAttribute("errorMessage", errorMessage);
return "user";
}
}
}
package com.timzu.spring.controller;

import com.timzu.spring.entity.User;
import com.timzu.spring.service.UserService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

private final int ROW_PER_PAGE = 5;

@Autowired
private UserService userService;

@Value("${msg.title}")
private String title;

@GetMapping(value = {"/", "/index"})
public String index(Model model) {
model.addAttribute("title", title);
return "index";
}

@GetMapping(value = "/users")
public String getUsers(Model model,
@RequestParam(value = "page", defaultValue = "1") int pageNumber) {
List<User> users = userService.findAll(pageNumber, ROW_PER_PAGE);

long count = userService.count();
boolean hasPrev = pageNumber > 1;
boolean hasNext = (pageNumber * ROW_PER_PAGE) < count;
model.addAttribute("users", users);
model.addAttribute("hasPrev", hasPrev);
model.addAttribute("prev", pageNumber - 1);
model.addAttribute("hasNext", hasNext);
model.addAttribute("next", pageNumber + 1);
return "user-list";
}

@GetMapping(value = "/users/{userId}")
public String getUserById(Model model, @PathVariable long userId) {
User user = null;
try {
user = userService.findById(userId);
model.addAttribute("allowDelete", false);
} catch (Exception ex) {
model.addAttribute("errorMessage", ex.getMessage());
}
model.addAttribute("user", user);
return "user";
}

@GetMapping(value = {"/users/add"})
public String showAddUser(Model model) {
User user = new User();
model.addAttribute("add", true);
model.addAttribute("user", user);

return "user-edit";
}

@PostMapping(value = "/users/add")
public String addUser(Model model,
@ModelAttribute("user") User user) {
try {
User newUser = userService.save(user);
return "redirect:/users/" + String.valueOf(newUser.getId());
} catch (Exception ex) {
// log exception first,
// then show error
String errorMessage = ex.getMessage();
logger.error(errorMessage);
model.addAttribute("errorMessage", errorMessage);

model.addAttribute("add", true);
return "user-edit";
}
}

@GetMapping(value = {"/users/{userId}/edit"})
public String showEditUser(Model model, @PathVariable long userId) {
User user = null;
try {
user = userService.findById(userId);
} catch (Exception ex) {
model.addAttribute("errorMessage", ex.getMessage());
}
model.addAttribute("add", false);
model.addAttribute("user", user);
return "user-edit";
}

@PostMapping(value = {"/users/{userId}/edit"})
public String updateUser(Model model,
@PathVariable long userId,
@ModelAttribute("user") User user) {
try {
user.setId(userId);
userService.update(user);
return "redirect:/users/" + String.valueOf(user.getId());
} catch (Exception ex) {
// log exception first,
// then show error
String errorMessage = ex.getMessage();
logger.error(errorMessage);
model.addAttribute("errorMessage", errorMessage);

model.addAttribute("add", false);
return "user-edit";
}
}

@GetMapping(value = {"/users/{userId}/delete"})
public String showDeleteUser(
Model model, @PathVariable long userId) {
User user = null;
try {
user = userService.findById(userId);
} catch (Exception ex) {
model.addAttribute("errorMessage", ex.getMessage());
}
model.addAttribute("allowDelete", true);
model.addAttribute("user", user);
return "user";
}

@PostMapping(value = {"/users/{userId}/delete"})
public String deleteUserById(
Model model, @PathVariable long userId) {
try {
userService.deleteById(userId);
return "redirect:/users";
} catch (Exception ex) {
String errorMessage = ex.getMessage();
logger.error(errorMessage);
model.addAttribute("errorMessage", errorMessage);
return "user";
}
}
}
Loading

0 comments on commit fe00a76

Please sign in to comment.