Lab3
This commit is contained in:
parent
70ec81dc5c
commit
17843eea1d
@ -14,6 +14,11 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,51 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.discipline.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.ulstu.is.cbapp.Lab3.discipline.model.Discipline;
|
||||
import ru.ulstu.is.cbapp.Lab3.discipline.service.DisciplineService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/discipline")
|
||||
public class DisciplineController {
|
||||
|
||||
private final DisciplineService disciplineService;
|
||||
|
||||
public DisciplineController(DisciplineService disciplineService) {
|
||||
this.disciplineService = disciplineService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Discipline getDiscipline(@PathVariable Long id) {
|
||||
return disciplineService.findDiscipline(id);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Discipline> getDisciplines() {
|
||||
return disciplineService.findAllDisciplines();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Discipline createDiscipline(@RequestParam("disciplineName") String disciplineName) {
|
||||
return disciplineService.addDiscipline(disciplineName);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Discipline updateDiscipline(@PathVariable Long id,
|
||||
@RequestParam("disciplineName") String disciplineName) {
|
||||
return disciplineService.updateDiscipline(id, disciplineName);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Discipline deleteDiscipline(@PathVariable Long id) {
|
||||
return disciplineService.deleteDiscipline(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.discipline.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Discipline {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column()
|
||||
private String disciplineName;
|
||||
|
||||
public Discipline() {
|
||||
}
|
||||
|
||||
public Discipline(String disciplineName) {
|
||||
this.disciplineName = disciplineName;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getDisciplineName() {
|
||||
return disciplineName;
|
||||
}
|
||||
|
||||
public void setDisciplineName(String disciplineName) {
|
||||
this.disciplineName = disciplineName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Discipline discipline = (Discipline) o;
|
||||
return Objects.equals(id, discipline.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Discipline{" +
|
||||
"id=" + id +
|
||||
", disciplineName='" + disciplineName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.discipline.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.cbapp.Lab3.discipline.model.Discipline;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class DisciplineService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Discipline addDiscipline(String disciplineName) {
|
||||
if (!StringUtils.hasText(disciplineName)) {
|
||||
throw new IllegalArgumentException("Discipline name is null or empty");
|
||||
}
|
||||
final Discipline discipline = new Discipline(disciplineName);
|
||||
em.persist(discipline);
|
||||
return discipline;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Discipline findDiscipline(Long id) {
|
||||
final Discipline discipline = em.find(Discipline.class, id);
|
||||
if (discipline == null) {
|
||||
throw new EntityNotFoundException(String.format("Discipline with id [%s] is not found", id));
|
||||
}
|
||||
return discipline;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Discipline> findAllDisciplines() {
|
||||
return em.createQuery("select s from Discipline s", Discipline.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Discipline updateDiscipline(Long id, String disciplineName) {
|
||||
if (!StringUtils.hasText(disciplineName)) {
|
||||
throw new IllegalArgumentException("Discipline name is null or empty");
|
||||
}
|
||||
final Discipline currentDiscipline = findDiscipline(id);
|
||||
currentDiscipline.setDisciplineName(disciplineName);
|
||||
return em.merge(currentDiscipline);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Discipline deleteDiscipline(Long id) {
|
||||
final Discipline currentDiscipline = findDiscipline(id);
|
||||
em.remove(currentDiscipline);
|
||||
return currentDiscipline;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllDisciplines() {
|
||||
em.createQuery("delete from Discipline").executeUpdate();
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.group.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.ulstu.is.cbapp.Lab3.group.model.Group;
|
||||
import ru.ulstu.is.cbapp.Lab3.group.service.GroupService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/group")
|
||||
public class GroupController {
|
||||
|
||||
private final GroupService groupService;
|
||||
|
||||
public GroupController(GroupService groupService) {
|
||||
this.groupService = groupService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Group getGroup(@PathVariable Long id) {
|
||||
return groupService.findGroup(id);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Group> getGroups() {
|
||||
return groupService.findAllGroups();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Group createGroup(@RequestParam("groupName") String groupName) {
|
||||
return groupService.addGroup(groupName);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Group updateGroup(@PathVariable Long id,
|
||||
@RequestParam("groupName") String groupName) {
|
||||
return groupService.updateGroup(id, groupName);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Group deleteGroup(@PathVariable Long id) {
|
||||
return groupService.deleteGroup(id);
|
||||
}
|
||||
}
|
57
src/main/java/ru/ulstu/is/cbapp/Lab3/group/model/Group.java
Normal file
57
src/main/java/ru/ulstu/is/cbapp/Lab3/group/model/Group.java
Normal file
@ -0,0 +1,57 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.group.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Group {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column()
|
||||
private String groupName;
|
||||
|
||||
public Group() {
|
||||
}
|
||||
|
||||
public Group(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
|
||||
public void setGroupName(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Group group = (Group) o;
|
||||
return Objects.equals(id, group.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Group{" +
|
||||
"id=" + id +
|
||||
", groupName='" + groupName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.group.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.cbapp.Lab3.group.model.Group;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class GroupService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Group addGroup(String groupName) {
|
||||
if (!StringUtils.hasText(groupName)) {
|
||||
throw new IllegalArgumentException("Group name is null or empty");
|
||||
}
|
||||
final Group group = new Group(groupName);
|
||||
em.persist(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Group findGroup(Long id) {
|
||||
final Group group = em.find(Group.class, id);
|
||||
if (group == null) {
|
||||
throw new EntityNotFoundException(String.format("Group with id [%s] is not found", id));
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Group> findAllGroups() {
|
||||
return em.createQuery("select s from Group s", Group.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Group updateGroup(Long id, String groupName) {
|
||||
if (!StringUtils.hasText(groupName)) {
|
||||
throw new IllegalArgumentException("Group name is null or empty");
|
||||
}
|
||||
final Group currentGroup = findGroup(id);
|
||||
currentGroup.setGroupName(groupName);
|
||||
return em.merge(currentGroup);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Group deleteGroup(Long id) {
|
||||
final Group currentGroup = findGroup(id);
|
||||
em.remove(currentGroup);
|
||||
return currentGroup;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllGroups() {
|
||||
em.createQuery("delete from Group").executeUpdate();
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.student.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.ulstu.is.cbapp.Lab3.student.model.Student;
|
||||
import ru.ulstu.is.cbapp.Lab3.student.service.StudentService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/student")
|
||||
public class StudentController {
|
||||
private final StudentService studentService;
|
||||
|
||||
|
||||
public StudentController(StudentService studentService) {
|
||||
this.studentService = studentService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Student getStudent(@PathVariable Long id) {
|
||||
return studentService.findStudent(id);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Student> getStudents() {
|
||||
return studentService.findAllStudents();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Student createStudent(@RequestParam("firstName") String firstName,
|
||||
@RequestParam("lastName") String lastName) {
|
||||
return studentService.addStudent(firstName, lastName);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Student updateStudent(@PathVariable Long id,
|
||||
@RequestParam("firstName") String firstName,
|
||||
@RequestParam("lastName") String lastName) {
|
||||
return studentService.updateStudent(id, firstName, lastName);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Student deleteStudent(@PathVariable Long id) {
|
||||
return studentService.deleteStudent(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.student.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Student {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column()
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Student(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Student student = (Student) o;
|
||||
return Objects.equals(id, student.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,66 @@
|
||||
package ru.ulstu.is.cbapp.Lab3.student.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.cbapp.Lab3.student.model.Student;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class StudentService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Student addStudent(String firstName, String lastName) {
|
||||
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName)) {
|
||||
throw new IllegalArgumentException("Student name is null or empty");
|
||||
}
|
||||
final Student student = new Student(firstName, lastName);
|
||||
em.persist(student);
|
||||
return student;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Student findStudent(Long id) {
|
||||
final Student student = em.find(Student.class, id);
|
||||
if (student == null) {
|
||||
throw new EntityNotFoundException(String.format("Student with id [%s] is not found", id));
|
||||
}
|
||||
return student;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Student> findAllStudents() {
|
||||
return em.createQuery("select s from Student s", Student.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Student updateStudent(Long id, String firstName, String lastName) {
|
||||
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName)) {
|
||||
throw new IllegalArgumentException("Student name is null or empty");
|
||||
}
|
||||
final Student currentStudent = findStudent(id);
|
||||
currentStudent.setFirstName(firstName);
|
||||
currentStudent.setLastName(lastName);
|
||||
return em.merge(currentStudent);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Student deleteStudent(Long id) {
|
||||
final Student currentStudent = findStudent(id);
|
||||
em.remove(currentStudent);
|
||||
return currentStudent;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllStudents() {
|
||||
em.createQuery("delete from Student").executeUpdate();
|
||||
}
|
||||
}
|
||||
|
64
src/test/java/ru/ulstu/is/cbapp/JPAStudentTests.java
Normal file
64
src/test/java/ru/ulstu/is/cbapp/JPAStudentTests.java
Normal file
@ -0,0 +1,64 @@
|
||||
package ru.ulstu.is.cbapp;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.ulstu.is.cbapp.Lab3.student.model.Student;
|
||||
import ru.ulstu.is.cbapp.Lab3.student.service.StudentService;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JPAStudentTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JPAStudentTests.class);
|
||||
|
||||
@Autowired
|
||||
private StudentService studentService;
|
||||
|
||||
@Test
|
||||
void testStudentCreate() {
|
||||
studentService.deleteAllStudents();
|
||||
final Student student = studentService.addStudent("Иван", "Иванов");
|
||||
log.info(student.toString());
|
||||
Assertions.assertNotNull(student.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStudentRead() {
|
||||
studentService.deleteAllStudents();
|
||||
final Student student = studentService.addStudent("Иван", "Иванов");
|
||||
log.info(student.toString());
|
||||
final Student findStudent = studentService.findStudent(student.getId());
|
||||
log.info(findStudent.toString());
|
||||
Assertions.assertEquals(student, findStudent);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStudentReadNotFound() {
|
||||
studentService.deleteAllStudents();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> studentService.findStudent(-1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStudentReadAll() {
|
||||
studentService.deleteAllStudents();
|
||||
studentService.addStudent("Иван", "Иванов");
|
||||
studentService.addStudent("Петр", "Петров");
|
||||
final List<Student> students = studentService.findAllStudents();
|
||||
log.info(students.toString());
|
||||
Assertions.assertEquals(students.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStudentReadAllEmpty() {
|
||||
studentService.deleteAllStudents();
|
||||
final List<Student> students = studentService.findAllStudents();
|
||||
log.info(students.toString());
|
||||
Assertions.assertEquals(students.size(), 0);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user