From 8595331a1dbdb3afcc37211b6a6157f7621f8031 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Fri, 14 Apr 2023 22:00:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.mv.db | Bin 32768 -> 32768 bytes .../IP/University/Service/GroupService.java | 4 ++ .../IP/University/Service/StudentService.java | 61 ++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java diff --git a/data.mv.db b/data.mv.db index da09d65c69633a60614dcdd7e82da47172c3e16d..faae2b61e1ce1be82a660c235403f6c21803587a 100644 GIT binary patch delta 2124 zcmd6o%}>*C7{~kT$iPj+gKi1}1q3Bl=zB{;6fl?&kq}W(6XUwJ$jjcsiopZ2qc@c& zUW_ChOpt>ITr^&d7vo8d2NO7YHR>Or`0HBRZ4v`U6L;9tWlf*w_k2I!Z*w^^mm}{q z?NVqxVQMLrz1!TjPoc*Xrln7qGpc4tk`Nd8(3(eSnCrtcY zI;mw;R>oycH5i6YnRAw^&>8KTnN@juCY`oajtDYO(soM8D|YnH|Xo;Om;Gz z!uMNgU5&?OSnE6!3K2x$d*Bj5*1&U0sgjK@xy#@M_)HzD(iBZ+8BxoJnwAj^w;YB{ z-)rdZJ&vzGa1~F8C_;ycARelNWt_r0MR1Df6v-)a{#9xY!OhGg=*cu75gLc_cm8eb zHv}!&#x{v$Wr83thHFTolBm=tW>eE0qFW`m$`H%eldEVk7J+-&I;e{&1GA~wtZCQ> zou9qdsC!_^JGxjnIxCnWo!0f)jF!@GsEoXV3cXYbhR?MxlDuyH?&qph5M*dII5SMDd#^V8O@mN3DNR~>>` z1>RWE$kx2BY0m`OT|{`T?3%k|+QG delta 1918 zcmeH{OKclO9L8tXv7M~rJlr58O2Vc|n#OF?ork>-A>x8mp+X_2ltZOzUv8{8yLMui za*B~SQb;?9UO*g>IB)- z{eSayIzp!-?9M`ZrFe9T%GJ8tve(@$gIbP*u%n9I=11dKI?r64c!F=FOrFYC-M2Rk zEZ1F+t+rMsFS=XXHQzI+-12S1#F%wYye^6Wq~1yW3WPlSPSV(4(&66p?B|f(IdX$N zn7clm`XPG{thlb>K*c7V0RKH9OLQ33)OhI23{@8WcjV z#X?9dgv#t&eQexT6uDlz=&ptqAa+w$x61pwYJbepHrSEbBCLe{>Q1Gt(cM{%rbnX~mK>qr^) z@I?0PfSayoAl}c05q0%x+}z)-Zy4X>OrG-#z5yAK76263Uge7dfAJgq9)ffqvJfgZ z0M)ZQL>n{`4@i1y&=UVq-~WH5%Y;abaMhNak`m9cskvfA4>oP?KCA#xyunYw$Jz(% z_xYJ@#xpnF<@5Ziy~b|)FR+!x>G84PozeCj{|*1bGGgZze|!$&fxD+d%2zm@`XmL0 zKw!bE$^MURAFHC2-_W4>#SbGH@4v!-R{*%Y$}1m#p6WLBTmV7R(xff@%+do0w1W_^ zFZBs_OV-UdPr45MmURm;d?H>HIjktS`kWLGVYVJ7OJ#v)&GJ7|UE5df>mBZrFNEfCLaUN1b=}dNbM9hum$Hpfn3x|4pl~Hky zN8Z44Qgv;$Dhl z`grdBpLb#K5{_n=Ktx3-3L_p=*mUqef~mL{v3c({2eJZE&8ZO0p>w<4=GoDJ%_K7u R*7|Ik<%0p$dw3p7j{puLGXDSo diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java new file mode 100644 index 0000000..fc8a8eb --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java @@ -0,0 +1,4 @@ +package ru.IP_LabWorks.IP.University.Service; + +public class GroupService { +} diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java index 65f6b5a..6fbe9fd 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java @@ -1,4 +1,65 @@ package ru.IP_LabWorks.IP.University.Service; +import jakarta.persistence.Entity; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import ru.IP_LabWorks.IP.University.Model.Student; + +import java.util.List; + +@Service public class StudentService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Student addStudent(String name, Integer age){ + if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(age))) { + throw new IllegalArgumentException("Student name or age is null or empty"); + } + final Student student = new Student(name, age); + 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 findAllStudents() { + return em.createQuery("select s from Student s", Student.class).getResultList(); + } + + @Transactional + public Student updateStudent(Long id, String name, Integer age) { + if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(age))) { + throw new IllegalArgumentException("Student name or age is null or empty"); + } + final Student currentStudent = findStudent(id); + currentStudent.setName(name); + currentStudent.setAge(age); + return em.merge(currentStudent); + } + + @Transactional + public Student deleteStudent(Long id) { + final Student currentStudent = findStudent(id); + em.remove(currentStudent); + return currentStudent; + } + + @Transactional + public void deleteAllStudent() { + em.createQuery("delete from Student").executeUpdate(); + } }