Implement user service and controller with CRUD functions master
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 23 Apr 2024 17:59:10 +0000 (20:59 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 23 Apr 2024 17:59:10 +0000 (20:59 +0300)
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.

src/main/java/eu/svjatoslav/alyverkko/controller/UserController.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/alyverkko/models/User.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/alyverkko/repository/UserRepository.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/alyverkko/service/UserService.java [new file with mode: 0644]
src/main/resources/application.properties
src/main/resources/templates/user/list.html [new file with mode: 0644]

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 (file)
index 0000000..2ac721d
--- /dev/null
@@ -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<User> 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 (file)
index 0000000..edba8c5
--- /dev/null
@@ -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 (file)
index 0000000..a29a790
--- /dev/null
@@ -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<User, Long> {
+
+
+
+}
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 (file)
index 0000000..8f0c113
--- /dev/null
@@ -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<User> 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();
+    }
+}
index 162c6fd..364702a 100644 (file)
@@ -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 (file)
index 0000000..dba5565
--- /dev/null
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>Your Entities List</title>
+</head>
+<body>
+<h1>Entities List</h1>
+<a class="btn btn-primary mb-3" href="users/new">Create New User</a>
+<table class="table">
+    <thead>
+    <tr>
+        <th>ID</th>
+        <th>Name</th>
+        <th>Actions</th>
+    </tr>
+    </thead>
+    <tbody>
+    <tr th:each="user : ${users}">
+        <td th:text="${user.id}"></td>
+        <td th:text="${user.username}"></td>
+        <td>
+            <a class="btn btn-info" th:href="@{/user/{id}(id=${user.id})}">View</a>
+            <a class="btn btn-primary" th:href="@{/your/{id}/edit}(id=${user.id})">Edit</a>
+        </td>
+    </tr>
+    </tbody>
+</table>
+</body>
+</html>