From: Svjatoslav Agejenko Date: Tue, 23 Apr 2024 17:59:10 +0000 (+0300) Subject: Implement user service and controller with CRUD functions X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=HEAD;p=alyverkko.git Implement user service and controller with CRUD functions This commit adds a new User model with corresponding service, controller, and repository classes. Also, a list.html file is created to handle user display in the frontend. Extra database configurations are added in the application.properties file. The new user service integrates basic CRUD operations while the controller manages user-related HTTP requests. --- diff --git a/src/main/java/eu/svjatoslav/alyverkko/controller/UserController.java b/src/main/java/eu/svjatoslav/alyverkko/controller/UserController.java new file mode 100644 index 0000000..2ac721d --- /dev/null +++ b/src/main/java/eu/svjatoslav/alyverkko/controller/UserController.java @@ -0,0 +1,81 @@ +package eu.svjatoslav.alyverkko.controller; + +import eu.svjatoslav.alyverkko.models.User; +import eu.svjatoslav.alyverkko.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/users") +public class UserController { + + private final UserService userService; + + @Autowired + public UserController(UserService userService) { + this.userService = userService; + } + + @GetMapping + public List getAllUsers() { + return userService.findAll(); + } + + @PostMapping + public User createUser(@RequestBody User user) { + return userService.save(user); + } + + @DeleteMapping("/{id}") + public void deleteUser(@PathVariable Long id) { + userService.deleteById(id); + } + + + @GetMapping + public String listEntities(Model model) { + model.addAttribute("users", userService.findAll()); + return "user/list"; + } + + @GetMapping("/{id}") + public String showEntity(@PathVariable Long id, Model model) { + User entity = userService.findById(id); + if (entity != null) { + model.addAttribute("user", entity); + return "user/show"; + } + return "redirect:/user"; + } + + @GetMapping("/new") + public String createEntityForm(Model model) { + model.addAttribute("entity", new User()); + return "user/edit"; + } + + @PostMapping + public String saveEntity(@ModelAttribute("entity") User user) { + userService.save(user); + return "redirect:/user"; + } + + @GetMapping("/{id}/edit") + public String editEntityForm(@PathVariable Long id, Model model) { + User entity = userService.findById(id); + if (entity != null) { + model.addAttribute("user", entity); + return "user/edit"; + } + return "redirect:/user"; + } + + @DeleteMapping("/{id}") + public String deleteEntity(@PathVariable Long id) { + userService.deleteById(id); + return "redirect:/user"; + } +} \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/alyverkko/models/User.java b/src/main/java/eu/svjatoslav/alyverkko/models/User.java new file mode 100644 index 0000000..edba8c5 --- /dev/null +++ b/src/main/java/eu/svjatoslav/alyverkko/models/User.java @@ -0,0 +1,19 @@ +package eu.svjatoslav.alyverkko.models; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import lombok.Getter; +import lombok.Setter; + +@Entity +@Getter +@Setter +public class User { + @Id + private Long id; + + @Column(nullable = false) + private String username; + +} diff --git a/src/main/java/eu/svjatoslav/alyverkko/repository/UserRepository.java b/src/main/java/eu/svjatoslav/alyverkko/repository/UserRepository.java new file mode 100644 index 0000000..a29a790 --- /dev/null +++ b/src/main/java/eu/svjatoslav/alyverkko/repository/UserRepository.java @@ -0,0 +1,10 @@ +package eu.svjatoslav.alyverkko.repository; + +import eu.svjatoslav.alyverkko.models.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { + + + +} diff --git a/src/main/java/eu/svjatoslav/alyverkko/service/UserService.java b/src/main/java/eu/svjatoslav/alyverkko/service/UserService.java new file mode 100644 index 0000000..8f0c113 --- /dev/null +++ b/src/main/java/eu/svjatoslav/alyverkko/service/UserService.java @@ -0,0 +1,33 @@ +package eu.svjatoslav.alyverkko.service; + +import eu.svjatoslav.alyverkko.models.User; +import eu.svjatoslav.alyverkko.repository.UserRepository; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserService { + + UserRepository userRepository; + + public UserService(UserRepository userRepository) { + this.userRepository = userRepository; + } + + public List findAll() { + return userRepository.findAll(); + } + + public User save(User user) { + return userRepository.save(user); + } + + public void deleteById(Long id) { + userRepository.deleteById(id); + } + + public User findById(Long id) { + return userRepository.findById(id).get(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 162c6fd..364702a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,11 @@ spring.application.name=alyverkko + +spring.datasource.url=jdbc:postgresql://localhost:5432/your_database +spring.datasource.username=your_username +spring.datasource.password=your_password + +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true + +spring.thymeleaf.cache=false diff --git a/src/main/resources/templates/user/list.html b/src/main/resources/templates/user/list.html new file mode 100644 index 0000000..dba5565 --- /dev/null +++ b/src/main/resources/templates/user/list.html @@ -0,0 +1,30 @@ + + + + + Your Entities List + + +

Entities List

+Create New User + + + + + + + + + + + + + + + +
IDNameActions
+ View + Edit +
+ +