This commit is contained in:
Vombit committed 2026-10-08 10:55:09 +05:00
1 parent 159e5d6911
commit fcbdcef568
21 files changed
+1934 -3

No files matched your search

@@ -0,0 +1,13 @@
package com.example.MyUiRestDbService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyUiRestDbServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyUiRestDbServiceApplication.class, args);
}
}
@@ -1,4 +1,64 @@
package com.example.MyUiRestDbService.controller;
import com.example.MyUiRestDbService.dao.StudentRepository;
import com.example.MyUiRestDbService.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import java.util.Optional;
@Controller
public class StudentController {
private final StudentRepository studentRepository;
@Autowired
public StudentController(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
@GetMapping({"/list", "/"})
public ModelAndView getAllStudents() {
ModelAndView mav = new ModelAndView("list-students");
mav.addObject("students", studentRepository.findAll());
return mav;
}
@GetMapping("/addStudentForm")
public ModelAndView addStudentForm() {
ModelAndView mav = new ModelAndView("add-student-form");
Student student = new Student();
mav.addObject("student", student);
return mav;
}
@PostMapping("/saveStudent")
public RedirectView saveStudent(@ModelAttribute Student student) {
studentRepository.save(student);
return new RedirectView("/list");
}
@GetMapping("/showUpdateForm")
public ModelAndView showUpdateForm(@RequestParam Integer studentId) {
ModelAndView mav = new ModelAndView("add-student-form");
Optional<Student> optionalStudent = studentRepository.findById(studentId);
Student student = new Student();
if (optionalStudent.isPresent()) {
student = optionalStudent.get();
}
mav.addObject("student", student);
return mav;
}
@GetMapping("/deleteStudent")
public RedirectView deleteStudent(@RequestParam Integer studentId, ModelAndView model) {
studentRepository.deleteById(studentId);
return new RedirectView("/list");
}
}
@@ -0,0 +1,64 @@
package com.example.MyUiRestDbService.controller;
import com.example.MyUiRestDbService.dao.StudyGroupRepository;
import com.example.MyUiRestDbService.entity.StudyGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import java.util.Optional;
@Controller
public class StudyGroupController {
private final StudyGroupRepository studyGroupRepository;
@Autowired
public StudyGroupController(StudyGroupRepository studyGroupRepository) {
this.studyGroupRepository = studyGroupRepository;
}
@GetMapping("/groupList")
public ModelAndView getAllGroups() {
ModelAndView mav = new ModelAndView("list-groups");
mav.addObject("groups", studyGroupRepository.findAll());
return mav;
}
@GetMapping("/addGroupForm")
public ModelAndView addGroupForm() {
ModelAndView mav = new ModelAndView("add-group-form");
StudyGroup group = new StudyGroup();
mav.addObject("group", group);
return mav;
}
@PostMapping("/saveGroup")
public RedirectView saveGroup(@ModelAttribute StudyGroup group) {
studyGroupRepository.save(group);
return new RedirectView("/groupList");
}
@GetMapping("/showGroupUpdateForm")
public ModelAndView showUpdateForm(@RequestParam Integer groupId) {
ModelAndView mav = new ModelAndView("add-group-form");
Optional<StudyGroup> optionalGroup = studyGroupRepository.findById(groupId);
StudyGroup group = new StudyGroup();
if (optionalGroup.isPresent()) {
group = optionalGroup.get();
}
mav.addObject("group", group);
return mav;
}
@GetMapping("/deleteGroup")
public RedirectView deleteGroup(@RequestParam Integer groupId, ModelAndView model) {
studyGroupRepository.deleteById(groupId);
return new RedirectView("/groupList");
}
}
@@ -1,4 +1,9 @@
package com.example.MyUiRestDbService.dao;
public interface StudentRepository {
import com.example.MyUiRestDbService.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
}
@@ -0,0 +1,9 @@
package com.example.MyUiRestDbService.dao;
import com.example.MyUiRestDbService.entity.StudyGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudyGroupRepository extends JpaRepository<StudyGroup, Integer> {
}
@@ -1,7 +1,9 @@
package com.labwork.MySpringBoot2Dbase.entity;
package com.example.MyUiRestDbService.entity;
import jakarta.persistence.*;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Entity
@@ -0,0 +1,27 @@
package com.example.MyUiRestDbService.entity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "STUDY_GROUPS")
public class StudyGroup {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "faculty")
private String faculty;
@Column(name = "course")
private Integer course;
}
@@ -0,0 +1,10 @@
spring.application.name=MyUiRestDbService
spring.datasource.url=jdbc:h2:file:./testdb/students
spring.jpa.open-in-view=false
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
@@ -0,0 +1,36 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Add Group</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<h3>Add New Study Group</h3>
<hr/>
<form th:action="@{/saveGroup}" th:object="${group}" method="POST">
<input type="text" th:field="*{name}" class="form-control col-4 mb-4" placeholder="Enter Group Name"/>
<input type="text" th:field="*{faculty}" class="form-control col-4 mb-4" placeholder="Enter Faculty"/>
<input type="text" th:field="*{course}" class="form-control col-4 mb-4" placeholder="Enter Course"/>
<button type="submit" class="btn btn-primary col-2">Save</button>
<input type="hidden" th:field="*{id}"/>
</form>
<hr/>
<a th:href="@{/groupList}">Back to list</a>
</div>
</body>
</html>
@@ -0,0 +1,38 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Add Student</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<h3>Add New Student</h3>
<hr/>
<form th:action="@{/saveStudent}" th:object="${student}" method="POST">
<input type="text" th:field="*{name}" class="form-control col-4 mb-4" placeholder="Enter Name"/>
<input type="text" th:field="*{surname}" class="form-control col-4 mb-4" placeholder="Enter Surname"/>
<input type="text" th:field="*{faculty}" class="form-control col-4 mb-4" placeholder="Enter Faculty"/>
<input type="text" th:field="*{age}" class="form-control col-4 mb-4" placeholder="Enter Age"/>
<button type="submit" class="btn btn-primary col-2">Save</button>
<input type="hidden" th:field="*{id}"/>
</form>
<hr/>
<a th:href="@{/list}">Back to list</a>
</div>
</body>
</html>
@@ -0,0 +1,65 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>List Groups</title>
<link rel="stylesheet"
type="text/css"
href="https://cdn.datatables.net/v/bs4/dt-1.10.25/datatables.min.css" />
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"
integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l"
crossorigin="anonymous" />
</head>
<body>
<div class="container">
<h3>List Study Groups</h3>
<hr/>
<a th:href="@{/addGroupForm}" class="btn btn-primary">Add Group</a>
<a th:href="@{/list}" class="btn btn-secondary ml-2">Students</a>
<br/><br/>
<table class="table table-bordered table-striped" id="groupTable">
<thead>
<tr>
<th>Name</th>
<th>Faculty</th>
<th>Course</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="group: ${groups}">
<td th:text="${group.name}" />
<td th:text="${group.faculty}" />
<td th:text="${group.course}" />
<td>
<a th:href="@{/showGroupUpdateForm(groupId=${group.id})}" class="btn btn-info">Update</a>
<a th:href="@{/deleteGroup(groupId=${group.id})}" class="btn btn-danger ml-2">Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.25/datatables.min.js"></script>
<script>
$(document).ready(function() {
$("#groupTable").DataTable({
'aoColumnDefs': [{
'bSortable': false,
'aTargets': [-1] /* 1st one, start by the right */
}]
});
})
</script>
</body>
</html>
@@ -0,0 +1,66 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>List Students</title>
<link rel="stylesheet"
type="text/css"
href="https://cdn.datatables.net/v/bs4/dt-1.10.25/datatables.min.css" />
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"
integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l"
crossorigin="anonymous" />
</head>
<body>
<div class="container">
<h3>List Students</h3>
<hr/>
<a th:href="@{/addStudentForm}" class="btn btn-primary">Add Student</a>
<a th:href="@{/groupList}" class="btn btn-secondary ml-2">Groups</a>
<br/><br/>
<table class="table table-bordered table-striped" id="studentTable">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Faculty</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr th:each="student: ${students}">
<td th:text="${student.surname}" />
<td th:text="${student.name}" />
<td th:text="${student.faculty}" />
<td th:text="${student.age}" />
<td>
<a th:href="@{/showUpdateForm(studentId=${student.id})}" class="btn btn-info">Update</a>
<a th:href="@{/deleteStudent(studentId=${student.id})}" class="btn btn-danger ml-2">Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.25/datatables.min.js"></script>
<script>
$(document).ready(function() {
$("#studentTable").DataTable({
'aoColumnDefs': [{
'bSortable': false,
'aTargets': [-1] /* 1st one, start by the right */
}]
});
})
</script>
</body>
</html>
@@ -0,0 +1,13 @@
package com.example.MyUiRestDbService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MyUiRestDbServiceApplicationTests {
@Test
void contextLoads() {
}
}