From 5568a57217c7fefc34b03b88abab19525e462d94 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Tue, 25 Apr 2023 00:35:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B5=D1=82=D0=B2=D1=91=D1=80=D1=82?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?.=20Backend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/ipLab/build.gradle | 1 + backend/ipLab/data.mv.db | Bin 73728 -> 81920 bytes .../Controllers/CustomerController.java | 56 ++++++++++++ .../Controllers/OrderedController.java | 58 ++++++++++++ .../Controllers/ProductController.java | 52 +++++++++++ .../Controllers/StoreController.java | 62 +++++++++++++ .../ipLab/StoreDataBase/DTO/CustomerDTO.java | 41 +++++++++ .../ipLab/StoreDataBase/DTO/OrderedDTO.java | 33 +++++++ .../ipLab/StoreDataBase/DTO/ProductDTO.java | 35 +++++++ .../ipLab/StoreDataBase/DTO/StoreDTO.java | 29 ++++++ .../Exceptions/CustomerNotFoundException.java | 7 ++ .../Exceptions/OrderedNotFoundException.java | 7 ++ .../Exceptions/ProductNotFoundException.java | 7 ++ .../Exceptions/StoreNotFoundException.java | 7 ++ .../ipLab/StoreDataBase/Model/Customer.java | 4 + .../ipLab/StoreDataBase/Model/Ordered.java | 13 --- .../ipLab/StoreDataBase/Model/Product.java | 2 + .../ipLab/StoreDataBase/Model/Store.java | 15 +-- .../Repositories/CustomerRepository.java | 7 ++ .../Repositories/OrderedRepository.java | 7 ++ .../Repositories/ProductRepository.java | 7 ++ .../Repositories/StoreRepository.java | 7 ++ .../Service/CustomerService.java | 44 ++++----- .../StoreDataBase/Service/OrderService.java | 86 ++++++------------ .../StoreDataBase/Service/ProductService.java | 43 ++++----- .../StoreDataBase/Service/StoreService.java | 40 ++++---- .../util/error/AdviceController.java | 42 +++++++++ .../util/validation/ValidationException.java | 9 ++ .../util/validation/ValidatorUtil.java | 30 ++++++ .../Configuration/CalcConfiguration.java | 23 ----- .../ipLab/TypesCalc/Service/CalcService.java | 33 ------- .../controller/TypeCalcController.java | 41 --------- .../ipLab/TypesCalc/domen/TypeCalc.java | 8 -- .../TypesCalc/domen/TypeCalcInteger.java | 30 ------ .../ipLab/TypesCalc/domen/TypeCalcString.java | 57 ------------ .../ipLab/controllers/CalcController.java | 39 -------- 36 files changed, 596 insertions(+), 386 deletions(-) create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/CustomerController.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/OrderedController.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/ProductController.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/StoreController.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/CustomerDTO.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/OrderedDTO.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/ProductDTO.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/StoreDTO.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/CustomerNotFoundException.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/OrderedNotFoundException.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/ProductNotFoundException.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/StoreNotFoundException.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/CustomerRepository.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/OrderedRepository.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/ProductRepository.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/StoreRepository.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/error/AdviceController.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidationException.java create mode 100644 backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidatorUtil.java delete mode 100644 backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java delete mode 100644 backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java delete mode 100644 backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java delete mode 100644 backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java delete mode 100644 backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java delete mode 100644 backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java delete mode 100644 backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java diff --git a/backend/ipLab/build.gradle b/backend/ipLab/build.gradle index 280d2a5..dec8f5c 100644 --- a/backend/ipLab/build.gradle +++ b/backend/ipLab/build.gradle @@ -22,6 +22,7 @@ dependencies { implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'org.hibernate.validator:hibernate-validator' } tasks.named('test') { diff --git a/backend/ipLab/data.mv.db b/backend/ipLab/data.mv.db index 5f57590383e2a74d41f33a0c6abaca82322b244c..a4a777a40054a7f4e342c7feb46c95c252aa5dbc 100644 GIT binary patch delta 7109 zcmeI0U2GKB702)05AS+e{DIdC2~MzM3Pf3xxp%&|LUp8Cp@Jlh+EP?nl7}i(ZQ7S6mRcoMAJXQvU-Zn~ote94 z0b|lCmDmT*9bbFq>^cAQyZ?Jj*CM4-blvVO&E$p)_K~bHliY;OnZ6woOxTB< z@odf@c_Qg{qC9rCb*9ul+m^tq`oqiJG0Ar1+OyTnurrb+nWNTdmJk!JC$g3#Wk#G6 z6Im@YYQ5kbgFwa$g^8?g*b;n*>g1Ko#Nne();0}Iu@pIT+!;T1xBw4=2Tv62tTB{l z<>$V%Ie}2*dUP7$ttbYs*hz>{yPB0<`6(Q7D0|b{0+-7BPAr@>!Vm;P;qSyA#_W?r zooPpEwhv8?9N9@6;*TinI{fF*%XoHr6Z_@OGjl&m;m6q0rH$;-WqGcp6K}wDuBQ|C zu=A%AbFntOf$bWIvumeQb6eVQ_W8+?$zx7F2)=s%)WkmjOHknpQT~NgE1xOY_T;EF zVxP=P1e0Vf9TLr-8^nX_`w{9(Blm+3{-!*3tcjIgi*FC|ET+h>qT*;o9sj`% zv+E$S)tYO(F0`&LL^o=M$<+!Y*9v30b&(V)Pqd&gDpiT_J)mSz7@}wjyzJ>1RHo&4 z>$b`p)2XH~CA4zmr33L8PPG04x3sg57fEP#fwc`@*%Doz$9tR;*3r>nCpx>}kD;XVVrO^bD*5%2dA%;V_1ZXm__*^7h)h+aj641^SD3>7aPuozK1Xm*SY5=k%bVtY zp2G3f7xn9(Mo}+ft~IXWY8h0f4fGc8qcLvPR>NiywMFZ+MYLqAZYaFKG%v|X-DE$0 zrN6gYa@n>F2NdUuuM7I7gu?Hf;UbKzPnX9c>$nI5Q3WDY0+eV(BMJ>fm@_@_)WIRg z;Q$Dz!oget2aCt(!a>lWVOx@*L6d7RC%bz0;P8C`4pt2gISvQxu&3Vc4mk#A!`Cce zLk^+n6}%OrHsq;GGrf@``y<64ss2dwN4h^UTxE8Y8`Qmgy0^Gy913Do72;%WRe>sG z#Yd*70=a_QJvI9EV^+zFYArChx3B_Wh;iw~ML%0=O(#|jR$gxt6+jg_z`Ng2noak>zvG^a`8j*IWa_~E<|d= z9Vz%He_DTF9Xm;Dg+T8Kj|keuc<8N^+UgLtXpOE9cC zb);7)silgqYN|qMgVgClSUL|&(-a{piBvlLunaGAvJe*}q1yaJSrHd_2KNJT8Nvz_ z#KGg@G`O`F#B{-bL%MV#78F+7A}qI6!0@jQ5ajiM8%{(IkHm%;9S1oR*$ycCt(Dy)S}<}iO%g;B^aB9)xhB5EZ?C-3ors1MiM@61GCQ$vKh`TZ z_WcTDlik?XQK{_zc2m<^EzLOD&g8|O(3wH@)y0{P=$XL*IzR?wghHp2-wXW!9M-#o zyW$_7`smdB)WTF{z4bTDHec*{B04{{XLP)fpR^}jdw`+~Q+sRol>Y)U`1_8%;q&l> znEg8Y$HnBv`6+j1f&X+9Jig)m$PZ!ux_uz^;VJadDKtNY7N*>(h?E|Q-ri&g$mb@b z!cF0!QWyKv&HhKK-Ov~$3Yd*<_V>~{7F%w=t=mW$pAl1V$?weSYEV1^_S|I}4VYF( z-Fi>I_T@tK#Y#YsqSJVNP*=K_73Sr>Ta|N0w&i-`1K#JI?;^DA8Vdh&147$DIOq4m zwI4-(z1!6hihTTDdCZCNE)H5x>>b~go0dj3-<5v=P^`2s7oWuT+)OJ@$8`gy&7hNQ zAK`U=sp=lqg7P3JAJfp!yPpTo9$kw35jA!{jdn^xd-$G5a755DpClZqhI+%b7YUu) zJOZ?3PfkC6?or%y_cq$B5BDx#>{)vZ9Ea*w55g~*fDQ2 zExN?U?e_m*$6oQJ;oC7|g&m`EogJ%pjNFd#0}10+S5^cbxVG%>u1pKy z^#U5TP0hQxlg;?T+Icl56- z6BWEKND9#AnGDbdeGb2fLKq`-vV@TK4So$B{O033<3`Rf4O5~B#V<53Rz6r-jN_$B z@n=u8z#+oMFNW(55$GKb5w1VNBC9+e+EY6oilT=Wqo}pf7I4)NblpPgr@k^h1`@Jn z2zKd1!Sg%_lIj7Q{D6Y)y4~o6>eJbj23tOFZ6~nVtG7r0#a(GpBz54zh0WEP;IeAA zwA626*t<6#PQzhP^e-*Y#Qmr#^AcPUiXGfn9{XL=^*}oWf`|z4kz;Grp^l^i3!Y{F zKAXq~JHiGwXDvI!;I;m)4_@~40RT_a3$f25)P7$b?i(KA;OBwisU6dcdVO&I^uIrU zy1U`IkMpNhIGxYt`4d0qxkm%GYDU&0lJgMtZL%i{<0nI5_qMk%&VGA!fJtxmu~)Bb ktGs>nb&Qv;79YaRO*;hoiQMvWnYR^HmL$u?WvDyxAJz1bga7~l delta 1748 zcmbtVT}&KR6rMY~3(JBF$QEnU77E2sJB7J3JHN4(CZ?JufN6~&Q0)9#BCz}m)hG`> z7<~{tElU7XUTjR02DVc)YNEsk8coxPFFu)8eL+LC)R@#Xw%*yF*&+I1;?q6%CUd^? zedl~p0k#US(astGo$_F-QpOd*V6h`JiWg@#xZuU zHDa8xhE!cQB_VFJk(8D!eXlH1`#)1$sV-zeo+$@yrYSaN4Gs4s2&ic>BMC#rhQvXt z_36S0MMcL-cRQb+E)F3U*6LCS!}!H^Bs%LCZy+SY`ePN2^WoWoy5;CosHBwo^@YWb zZ#&-X{HSxIbFS-p*Zr>LQ$LK>fmVAwn_mHKgG9e?_Tsi9W8U4hn=da!BV8a^o7cB zTof^H8aStJ)HJf{>aM!*`S~Y+n|HBV50Wf(sF|V>qNrEMMeInQba&T*hOn&5imb34 zMTHlOK5%ZXUI?SNoj1N}i0*I#Xg^ihrwue<9M7JDbK}|95pBxQa-xbHPU1vipd=o^ zSRfP(!s?pFTQW1+KRRrg>AS7#klLzHP0cr>QfAN; zUSSf3F*>028xyK+v(ja+2su}T1efpBA|BT7>_j5`a6VS7#W99gT1L#c=@*mb7d^^)t^_0FdZxE$%xZRfD!P7* zfjXzaCtJm|sl(JJJz!}?HDg_n)2es}i@n(5jxO!i;!&(IQ;9$uI_R~gF>xC@>fI)ZSL<wW(w Ue@))@R5TuQ8teWTTIlM?-^j5}WdHyG diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/CustomerController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/CustomerController.java new file mode 100644 index 0000000..1da68d3 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/CustomerController.java @@ -0,0 +1,56 @@ +package com.example.ipLab.StoreDataBase.Controllers; + +import com.example.ipLab.StoreDataBase.DTO.CustomerDTO; +import com.example.ipLab.StoreDataBase.Model.Customer; +import com.example.ipLab.StoreDataBase.Service.CustomerService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/customer") +public class CustomerController { + private final CustomerService customerService; + + public CustomerController(CustomerService customerService){ + this.customerService = customerService; + } + + @GetMapping("/{id}") + public CustomerDTO getCustomer(@PathVariable Long id){ + return new CustomerDTO(customerService.getCustomer(id)); + } + + @GetMapping + public List getCustomers(){ + return customerService.getAllCustomers().stream() + .map(CustomerDTO::new) + .toList(); + } + + @PostMapping + public CustomerDTO createCustomer(@RequestParam("customerLastName") String customerLastName, + @RequestParam("customerFirstName") String customerFirstName, + @RequestParam("customerMiddleName") String customerMiddleName){ + final Customer customer = customerService.addCustomer(customerLastName, customerFirstName, customerMiddleName); + return new CustomerDTO(customer); + } + + @PutMapping("/{id}") + public CustomerDTO updateCustomer(@RequestParam("customerLastName") String customerLastName, + @RequestParam("customerFirstName") String customerFirstName, + @RequestParam("customerMiddleName") String customerMiddleName, + @PathVariable Long id){ + return new CustomerDTO(customerService.updateCustomer(id, customerLastName, customerFirstName, customerMiddleName)); + } + + @DeleteMapping("/{id}") + public CustomerDTO deleteCustomer(@PathVariable Long id){ + return new CustomerDTO(customerService.deleteCustomer(id)); + } + + @DeleteMapping() + public void deleteAllCustomers(){ + customerService.deleteAllCustomers(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/OrderedController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/OrderedController.java new file mode 100644 index 0000000..93e7da5 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/OrderedController.java @@ -0,0 +1,58 @@ +package com.example.ipLab.StoreDataBase.Controllers; + +import com.example.ipLab.StoreDataBase.DTO.OrderedDTO; +import com.example.ipLab.StoreDataBase.Model.Ordered; +import com.example.ipLab.StoreDataBase.Service.CustomerService; +import com.example.ipLab.StoreDataBase.Service.OrderService; +import com.example.ipLab.StoreDataBase.Service.ProductService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +public class OrderedController { + private final OrderService orderedService; + private final ProductService productService; + private final CustomerService customerService; + + public OrderedController(OrderService orderedService, ProductService productService, CustomerService customerService){ + this.productService = productService; + this.customerService = customerService; + this.orderedService = orderedService; + } + + @GetMapping("/{id}") + public OrderedDTO getOrdered(@PathVariable Long id){ + return new OrderedDTO(orderedService.getOrder(id)); + } + + @GetMapping + public List getOrdereds(){ + return orderedService.getAllOrders().stream() + .map(OrderedDTO::new) + .toList(); + } + + @PostMapping + public OrderedDTO createOrdered(@RequestParam("productId") Long productId, + @RequestParam("customerId") Long customerId, + @RequestParam("quantity") Integer quantity){ + final Ordered ordered = orderedService.addOrder(productService.getProduct(productId), customerService.getCustomer(customerId), quantity); + return new OrderedDTO(ordered); + } + + @PutMapping("/{id}") + public OrderedDTO updateOrdered(@RequestParam("quantity") Integer quantity, + @PathVariable Long id){ + return new OrderedDTO(orderedService.updateOrder(id, quantity)); + } + + @DeleteMapping("/{id}") + public OrderedDTO deleteOrdered(@PathVariable Long id){ + return new OrderedDTO(orderedService.deleteOrder(id)); + } + + @DeleteMapping() + public void deleteAllOrdereds(){ + orderedService.deleteAllOrders(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/ProductController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/ProductController.java new file mode 100644 index 0000000..624a5c7 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/ProductController.java @@ -0,0 +1,52 @@ +package com.example.ipLab.StoreDataBase.Controllers; + +import com.example.ipLab.StoreDataBase.DTO.ProductDTO; +import com.example.ipLab.StoreDataBase.Model.Product; +import com.example.ipLab.StoreDataBase.Service.ProductService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/product") +public class ProductController { + private final ProductService productService; + + public ProductController(ProductService productService){ + this.productService = productService; + } + + @GetMapping("/{id}") + public ProductDTO getProduct(@PathVariable Long id){ + return new ProductDTO(productService.getProduct(id)); + } + + @GetMapping + public List getProducts(){ + return productService.getAllProducts().stream() + .map(ProductDTO::new) + .toList(); + } + + @PostMapping + public ProductDTO createProduct(@RequestParam("productName") String productName){ + final Product product = productService.addProduct(productName); + return new ProductDTO(product); + } + + @PutMapping("/{id}") + public ProductDTO updateProduct(@RequestParam("productName") String productName, + @PathVariable Long id){ + return new ProductDTO(productService.updateProduct(id, productName)); + } + + @DeleteMapping("/{id}") + public ProductDTO deleteProduct(@PathVariable Long id){ + return new ProductDTO(productService.deleteProduct(id)); + } + + @DeleteMapping() + public void deleteAllProducts(){ + productService.deleteAllProducts(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/StoreController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/StoreController.java new file mode 100644 index 0000000..2d3373a --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Controllers/StoreController.java @@ -0,0 +1,62 @@ +package com.example.ipLab.StoreDataBase.Controllers; + +import com.example.ipLab.StoreDataBase.DTO.CustomerDTO; +import com.example.ipLab.StoreDataBase.DTO.ProductDTO; +import com.example.ipLab.StoreDataBase.DTO.StoreDTO; +import com.example.ipLab.StoreDataBase.Model.Customer; +import com.example.ipLab.StoreDataBase.Model.Store; +import com.example.ipLab.StoreDataBase.Service.CustomerService; +import com.example.ipLab.StoreDataBase.Service.StoreService; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/store") +public class StoreController { + private final StoreService storeService; + + public StoreController(StoreService storeService){ + this.storeService = storeService; + } + + @GetMapping("/{id}") + public StoreDTO getStore(@PathVariable Long id){ + return new StoreDTO(storeService.getStore(id)); + } + + @GetMapping + public List getStores(){ + return storeService.getAllStores().stream() + .map(StoreDTO::new) + .toList(); + } + + @PostMapping + public StoreDTO createStore(@RequestParam("storeName") String storeName){ + final Store store = storeService.addStore(storeName); + return new StoreDTO(store); + } + + @PutMapping("/{id}") + public StoreDTO updateStore(@RequestParam("storeName") String storeName, + @PathVariable Long id){ + return new StoreDTO(storeService.updateStore(id, storeName)); + } + + @PutMapping("/{storeId}-{productId}") + public ProductDTO addProduct(@PathVariable Long storeId, + @PathVariable Long productId){ + return new ProductDTO(storeService.addProduct(storeId, productId)); + } + + @DeleteMapping("/{id}") + public StoreDTO deleteStore(@PathVariable Long id){ + return new StoreDTO(storeService.deleteStore(id)); + } + + @DeleteMapping() + public void deleteAllStores(){ + storeService.deleteAllStores(); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/CustomerDTO.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/CustomerDTO.java new file mode 100644 index 0000000..92ad085 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/CustomerDTO.java @@ -0,0 +1,41 @@ +package com.example.ipLab.StoreDataBase.DTO; + +import com.example.ipLab.StoreDataBase.Model.Customer; + +import java.util.List; + +public class CustomerDTO { + public final Long id; + public final String lastname; + public final String firstname; + public final String middleName; + public final List orders; + + public CustomerDTO(Customer customer){ + this.id = customer.getId(); + this.lastname = customer.getLastName(); + this.firstname = customer.getFirstName(); + this.middleName = customer.getMiddleName(); + this.orders = customer.getOrders().stream().map(OrderedDTO::new).toList(); + } + + public Long getId() { + return id; + } + + public String getLastname() { + return lastname; + } + + public String getFirstname() { + return firstname; + } + + public String getMiddleName() { + return middleName; + } + + public List getOrders() { + return orders; + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/OrderedDTO.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/OrderedDTO.java new file mode 100644 index 0000000..df6872c --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/OrderedDTO.java @@ -0,0 +1,33 @@ +package com.example.ipLab.StoreDataBase.DTO; + +import com.example.ipLab.StoreDataBase.Model.Ordered; + +public class OrderedDTO { + public final Long id; + public final int quantity; + public final ProductDTO product; + public final CustomerDTO customer; + + public OrderedDTO(Ordered ordered){ + this.id = ordered.getId(); + this.quantity = ordered.getQuantity(); + this.product = new ProductDTO(ordered.getProduct()); + this.customer = new CustomerDTO(ordered.getCustomer()); + } + + public Long getId() { + return id; + } + + public int getQuantity() { + return quantity; + } + + public ProductDTO getProduct() { + return product; + } + + public CustomerDTO getCustomer() { + return customer; + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/ProductDTO.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/ProductDTO.java new file mode 100644 index 0000000..bd8552a --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/ProductDTO.java @@ -0,0 +1,35 @@ +package com.example.ipLab.StoreDataBase.DTO; + +import com.example.ipLab.StoreDataBase.Model.Product; + +import java.util.List; + +public class ProductDTO { + public final Long id; + public final String name; + public final StoreDTO store; + public final List orders; + + public ProductDTO(Product product){ + this.id = product.getId(); + this.name = product.getName(); + this.store = new StoreDTO(product.getStore()); + this.orders = product.getOrders().stream().map(OrderedDTO::new).toList(); + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public StoreDTO getStore() { + return store; + } + + public List getOrders() { + return orders; + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/StoreDTO.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/StoreDTO.java new file mode 100644 index 0000000..a03e101 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/DTO/StoreDTO.java @@ -0,0 +1,29 @@ +package com.example.ipLab.StoreDataBase.DTO; + +import com.example.ipLab.StoreDataBase.Model.Store; + +import java.util.List; + +public class StoreDTO { + public final Long id; + public final String storeName; + public final List products; + + public StoreDTO(Store store){ + this.id = store.getId(); + this.storeName = store.getStoreName(); + this.products = store.getProducts().stream().map(ProductDTO::new).toList(); + } + + public Long getId() { + return id; + } + + public String getStoreName() { + return storeName; + } + + public List getProducts() { + return products; + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/CustomerNotFoundException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/CustomerNotFoundException.java new file mode 100644 index 0000000..469aa78 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/CustomerNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Exceptions; + +public class CustomerNotFoundException extends RuntimeException{ + public CustomerNotFoundException(Long id){ + super(String.format("Customer with id: %s hasn't been found", id)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/OrderedNotFoundException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/OrderedNotFoundException.java new file mode 100644 index 0000000..cc42154 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/OrderedNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Exceptions; + +public class OrderedNotFoundException extends RuntimeException{ + public OrderedNotFoundException(Long id){ + super(String.format("Order with id: %s hasn't been found", id)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/ProductNotFoundException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/ProductNotFoundException.java new file mode 100644 index 0000000..0356019 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/ProductNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Exceptions; + +public class ProductNotFoundException extends RuntimeException{ + public ProductNotFoundException(Long id){ + super(String.format("Product with id: %s hasn't been found", id)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/StoreNotFoundException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/StoreNotFoundException.java new file mode 100644 index 0000000..4ffdcfc --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Exceptions/StoreNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Exceptions; + +public class StoreNotFoundException extends RuntimeException{ + public StoreNotFoundException(Long id){ + super(String.format("Store with id: %s hasn't been found", id)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java index 6605858..4743383 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Customer.java @@ -1,6 +1,7 @@ package com.example.ipLab.StoreDataBase.Model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; import java.util.ArrayList; import java.util.List; @@ -12,10 +13,13 @@ public class Customer { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column + @NotBlank(message = "Customer's last name can't be empty") private String lastName; @Column + @NotBlank(message = "Customer's first name can't be empty") private String firstName; @Column + @NotBlank(message = "Customer's middle name can't be empty") private String middleName; @OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL) private List orders; diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java index 653ebd9..0eeeb44 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Ordered.java @@ -9,9 +9,6 @@ public class Ordered { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name="store_fk") - private Store store; - @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name="customer_fk") private Customer customer; @ManyToOne(fetch = FetchType.EAGER) @@ -28,9 +25,6 @@ public class Ordered { return id; } - public Store getStore() { - return store; - } public Customer getCustomer() { return customer; @@ -55,13 +49,6 @@ public class Ordered { this.quantity = quantity; } - public void setStore(Store store) { - this.store = store; - if (!store.getOrders().contains(this)){ - store.AddOrdered(this); - } - } - public void setCustomer(Customer customer) { this.customer = customer; if (!customer.getOrders().contains(this)){ diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java index 3136b10..fe0cac5 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Product.java @@ -1,6 +1,7 @@ package com.example.ipLab.StoreDataBase.Model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; import java.util.ArrayList; import java.util.List; @@ -12,6 +13,7 @@ public class Product { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column + @NotBlank(message = "Product's name can't be empty") private String name; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "store_fk") diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java index b9ddadb..d3d7652 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Model/Store.java @@ -2,6 +2,7 @@ package com.example.ipLab.StoreDataBase.Model; import com.example.ipLab.StoreDataBase.Service.ProductService; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; import java.util.ArrayList; import java.util.List; @@ -13,15 +14,13 @@ public class Store { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column + @NotBlank(message = "Store's name can't be empty") private String storeName; @OneToMany(fetch = FetchType.EAGER, mappedBy = "store", cascade = CascadeType.ALL, orphanRemoval = true) private List products; - @OneToMany(fetch = FetchType.EAGER, mappedBy = "store", cascade = CascadeType.ALL) - private List orders; public Store(){} public Store(String storeName){ this.storeName = storeName; - this.orders = new ArrayList<>(); this.products = new ArrayList<>(); } @@ -33,10 +32,6 @@ public class Store { return storeName; } - public List getOrders() { - return orders; - } - public List getProducts() { return products; } @@ -46,12 +41,6 @@ public class Store { product.setStore(this); } } - public void AddOrdered(Ordered ordered){ - this.orders.add(ordered); - if (ordered.getStore() != this){ - ordered.setStore(this); - } - } public void setStoreName(String storeName) { this.storeName = storeName; diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/CustomerRepository.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/CustomerRepository.java new file mode 100644 index 0000000..2858381 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/CustomerRepository.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Repositories; + +import com.example.ipLab.StoreDataBase.Model.Customer; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CustomerRepository extends JpaRepository { +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/OrderedRepository.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/OrderedRepository.java new file mode 100644 index 0000000..45a6129 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/OrderedRepository.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Repositories; + +import com.example.ipLab.StoreDataBase.Model.Ordered; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface OrderedRepository extends JpaRepository { +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/ProductRepository.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/ProductRepository.java new file mode 100644 index 0000000..47ea075 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/ProductRepository.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Repositories; + +import com.example.ipLab.StoreDataBase.Model.Product; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProductRepository extends JpaRepository { +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/StoreRepository.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/StoreRepository.java new file mode 100644 index 0000000..530be3c --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Repositories/StoreRepository.java @@ -0,0 +1,7 @@ +package com.example.ipLab.StoreDataBase.Repositories; + +import com.example.ipLab.StoreDataBase.Model.Store; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface StoreRepository extends JpaRepository { +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/CustomerService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/CustomerService.java index 084074c..a701a1f 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/CustomerService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/CustomerService.java @@ -1,6 +1,9 @@ package com.example.ipLab.StoreDataBase.Service; +import com.example.ipLab.StoreDataBase.Exceptions.CustomerNotFoundException; import com.example.ipLab.StoreDataBase.Model.Customer; +import com.example.ipLab.StoreDataBase.Repositories.CustomerRepository; +import com.example.ipLab.StoreDataBase.util.validation.ValidatorUtil; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; @@ -12,53 +15,50 @@ import java.util.List; @Service public class CustomerService { - @PersistenceContext - private EntityManager em; + private final CustomerRepository customerRepository; + private final ValidatorUtil validatorUtil; + + public CustomerService(CustomerRepository customerRepository, + ValidatorUtil validatorUtil){ + this.customerRepository = customerRepository; + this.validatorUtil = validatorUtil; + } @Transactional public Customer addCustomer(String customerLastName, String customerFirstName, String customerMiddleName){ - if (!StringUtils.hasText(customerLastName) || !StringUtils.hasText(customerFirstName) || !StringUtils.hasText(customerMiddleName)){ - throw new IllegalArgumentException("Customer name is null or empty"); - } - final Customer customer = new Customer(customerLastName, customerFirstName, customerMiddleName); - em.persist(customer); - return customer; + Customer customer = new Customer(customerLastName, customerFirstName, customerMiddleName); + validatorUtil.validate(customer); + return customerRepository.save(customer); } @Transactional() public Customer getCustomer(Long id){ - Customer customer = em.find(Customer.class, id); - if (customer == null){ - throw new EntityNotFoundException(String.format("Customer with id = %s isn't found", id)); - } - return customer; + return customerRepository.findById(id).orElseThrow(() -> new CustomerNotFoundException(id)); } @Transactional public List getAllCustomers(){ - return em.createQuery("SELECT c from Customer c", Customer.class).getResultList(); + return customerRepository.findAll(); } @Transactional public Customer updateCustomer(Long id, String customerLastName, String customerFirstName, String customerMiddleName){ - if (!StringUtils.hasText(customerLastName) || !StringUtils.hasText(customerFirstName) || !StringUtils.hasText(customerMiddleName)){ - throw new IllegalArgumentException("Customer name is null or empty"); - } - final Customer customer = getCustomer(id); + Customer customer = getCustomer(id); customer.setLastName(customerLastName); customer.setFirstName(customerFirstName); customer.setMiddleName(customerMiddleName); - return em.merge(customer); + validatorUtil.validate(customer); + return customerRepository.save(customer); } @Transactional public Customer deleteCustomer(Long id){ - final Customer customer = getCustomer(id); - em.remove(customer); + Customer customer = getCustomer(id); + customerRepository.delete(customer); return customer; } @Transactional public void deleteAllCustomers(){ - em.createQuery("delete from Customer"); + customerRepository.deleteAll(); } } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java index b376f00..acbe26c 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/OrderService.java @@ -1,115 +1,81 @@ package com.example.ipLab.StoreDataBase.Service; +import com.example.ipLab.StoreDataBase.Exceptions.OrderedNotFoundException; import com.example.ipLab.StoreDataBase.Model.Customer; import com.example.ipLab.StoreDataBase.Model.Ordered; import com.example.ipLab.StoreDataBase.Model.Product; import com.example.ipLab.StoreDataBase.Model.Store; +import com.example.ipLab.StoreDataBase.Repositories.OrderedRepository; +import com.example.ipLab.StoreDataBase.util.validation.ValidatorUtil; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; +import jakarta.persistence.criteria.Order; import jakarta.transaction.Transactional; -import org.aspectj.weaver.ast.Or; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; -import java.util.ArrayList; import java.util.List; @Service public class OrderService { - @PersistenceContext - private EntityManager em; + private final OrderedRepository orderedRepository; + private final ValidatorUtil validatorUtil; + + public OrderService(OrderedRepository orderedRepository, + ValidatorUtil validatorUtil, + ProductService productService){ + this.productService = productService; + this.orderedRepository = orderedRepository; + this.validatorUtil = validatorUtil; + } private ProductService productService; - public OrderService(ProductService productService){ - this.productService = productService; - } - @Transactional - public Ordered addOrder(Store store, Product product, Customer customer, int quantity){ + public Ordered addOrder(Product product, Customer customer, int quantity){ final Ordered order = new Ordered(quantity); + validatorUtil.validate(order); product.AddOrdered(order); customer.AddOrdered(order); - store.AddOrdered(order); - em.persist(order); + orderedRepository.save(order); return order; } @Transactional() public Ordered getOrder(Long id){ - Ordered order = em.find(Ordered.class, id); - if (order == null){ - throw new EntityNotFoundException(String.format("Ordered with id = %s isn't found", id)); - } - return order; - } - @Transactional - public List getOrdersWithProduct(Long productId, int minQuantity, int maxQuantity){ - return em.createQuery("SELECT o FROM Ordered o WHERE o.product.id = ?1 AND o.quantity >= ?2 AND o.quantity <= ?3",Ordered.class) - .setParameter(1, productId) - .setParameter(2, minQuantity) - .setParameter(3, maxQuantity) - .getResultList(); + return orderedRepository.findById(id).orElseThrow(() -> new OrderedNotFoundException(id)); } @Transactional public List getAllOrders(){ - return em.createQuery("SELECT o FROM Ordered o", Ordered.class).getResultList(); + return orderedRepository.findAll(); } @Transactional public Ordered updateOrder(Long id, int quantity){ final Ordered order = getOrder(id); order.setQuantity(quantity); - return em.merge(order); + validatorUtil.validate(order); + return orderedRepository.save(order); } @Transactional public Ordered deleteOrder(Long id){ final Ordered order = getOrder(id); - order.getStore().getOrders().remove(order); order.getCustomer().getOrders().remove(order); - em.remove(order); + orderedRepository.delete(order); return order; } @Transactional public void deleteAllOrders(){ - em.createQuery("delete from Ordered").executeUpdate(); + orderedRepository.deleteAll(); } //product section @Transactional() - public Product getProduct(Long productId, Long orderId){ - Ordered order = em.find(Ordered.class, orderId); - if (order != null) { - return order.getProduct(); - } - else throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); - } - - @Transactional - public Product updateProduct(Long orderId, Long productId, String productName){ - if (!StringUtils.hasText(productName)){ - throw new IllegalArgumentException("Product name is null or empty"); - } - final Product product = getProduct(productId, orderId); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); - } - product.setName(productName); - return em.merge(product); - } - - @Transactional - public Product deleteProduct(Long orderId, Long productId){ - final Product product = getProduct(productId, orderId); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", productId)); - } - var order = product.getOrders().stream().filter(or -> or.getId() == orderId).findFirst(); - if (order.isPresent()) order.get().setProduct(null); - else throw new EntityNotFoundException(String.format("Order with id = %s isn't found", orderId)); - return product; + public Product getProduct(Long orderId){ + Ordered order = getOrder(orderId); + return order.getProduct(); } } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java index 286aa54..748c713 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/ProductService.java @@ -1,9 +1,12 @@ package com.example.ipLab.StoreDataBase.Service; +import com.example.ipLab.StoreDataBase.Exceptions.ProductNotFoundException; import com.example.ipLab.StoreDataBase.Model.Customer; import com.example.ipLab.StoreDataBase.Model.Ordered; import com.example.ipLab.StoreDataBase.Model.Product; import com.example.ipLab.StoreDataBase.Model.Store; +import com.example.ipLab.StoreDataBase.Repositories.ProductRepository; +import com.example.ipLab.StoreDataBase.util.validation.ValidatorUtil; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; @@ -14,59 +17,49 @@ import org.springframework.util.StringUtils; import java.util.List; @Service public class ProductService { - @PersistenceContext - private EntityManager em; + private final ProductRepository productRepository; + private final ValidatorUtil validatorUtil; + + public ProductService(ProductRepository productRepository, ValidatorUtil validatorUtil){ + this.productRepository = productRepository; + this.validatorUtil = validatorUtil; + } @Transactional public Product addProduct(String productName){ - if (!StringUtils.hasText(productName)){ - throw new IllegalArgumentException("Product name is null or empty"); - } final Product product = new Product(productName); - em.persist(product); + validatorUtil.validate(product); + productRepository.save(product); return product; } @Transactional() public Product getProduct(Long id){ - Product product = em.find(Product.class, id); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); - } - em.persist(product); - return product; + return productRepository.findById(id).orElseThrow(() -> new ProductNotFoundException(id)); } @Transactional public List getAllProducts(){ - return em.createQuery("SELECT p FROM Product p", Product.class).getResultList(); + return productRepository.findAll(); } @Transactional public Product updateProduct(Long id, String productName){ - if (!StringUtils.hasText(productName)){ - throw new IllegalArgumentException("Product name is null or empty"); - } final Product product = getProduct(id); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); - } product.setName(productName); - return em.merge(product); + validatorUtil.validate(product); + return productRepository.save(product); } @Transactional public Product deleteProduct(Long id){ final Product product = getProduct(id); - if (product == null){ - throw new EntityNotFoundException(String.format("Product with id = %s isn't found", id)); - } Store store = product.getStore(); if (store != null) store.getProducts().remove(product); - em.remove(product); + productRepository.delete(product); return product; } @Transactional public void deleteAllProducts(){ - em.createQuery("delete from Product"); + productRepository.deleteAll(); } } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java index 63229c6..b1b1dd0 100644 --- a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/Service/StoreService.java @@ -1,7 +1,10 @@ package com.example.ipLab.StoreDataBase.Service; +import com.example.ipLab.StoreDataBase.Exceptions.StoreNotFoundException; import com.example.ipLab.StoreDataBase.Model.Product; import com.example.ipLab.StoreDataBase.Model.Store; +import com.example.ipLab.StoreDataBase.Repositories.StoreRepository; +import com.example.ipLab.StoreDataBase.util.validation.ValidatorUtil; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; @@ -14,57 +17,51 @@ import java.util.List; @Service public class StoreService { - @PersistenceContext - private EntityManager em; + private final StoreRepository storeRepository; + private final ValidatorUtil validatorUtil; private ProductService productService; - public StoreService(ProductService productService){ + public StoreService(StoreRepository storeRepository, ValidatorUtil validatorUtil, ProductService productService){ + this.storeRepository = storeRepository; + this.validatorUtil = validatorUtil; this.productService = productService; } @Transactional public Store addStore(String storeName){ - if (!StringUtils.hasText(storeName)){ - throw new IllegalArgumentException("Store name is null or empty"); - } final Store store = new Store(storeName); - em.persist(store); + validatorUtil.validate(store); + storeRepository.save(store); return store; } @Transactional() public Store getStore(Long id){ - Store store = em.find(Store.class, id); - if (store == null){ - throw new EntityNotFoundException(String.format("Store with id = %s isn't found", id)); - } - return store; + return storeRepository.findById(id).orElseThrow(() -> new StoreNotFoundException(id)); } @Transactional public List getAllStores(){ - return em.createQuery("SELECT s FROM Store s", Store.class).getResultList(); + return storeRepository.findAll(); } @Transactional public Store updateStore(Long id, String storeName){ - if (!StringUtils.hasText(storeName)){ - throw new IllegalArgumentException("Store name is null or empty"); - } final Store store = getStore(id); store.setStoreName(storeName); - return em.merge(store); + validatorUtil.validate(store); + return storeRepository.save(store); } @Transactional public Store deleteStore(Long id){ final Store store = getStore(id); - em.remove(store); + storeRepository.delete(store); return store; } @Transactional public void deleteAllStores(){ - em.createQuery("delete from Store"); + storeRepository.deleteAll(); } //product section @@ -73,7 +70,7 @@ public class StoreService { Store store = getStore(storeId); Product product = productService.getProduct(productId); store.AddProduct(product); - em.persist(store); + storeRepository.save(store); return product; } @@ -98,7 +95,7 @@ public class StoreService { Store store = getStore(storeId); Product product = getProductFromStore(productId, storeId); store.getProducts().remove(product); - em.remove(product); + product.setStore(null); return product; } @Transactional @@ -109,7 +106,6 @@ public class StoreService { storeProducts) { pr.setStore(null); store.getProducts().remove(pr); - em.remove(pr); } } } diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/error/AdviceController.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/error/AdviceController.java new file mode 100644 index 0000000..d9a5aa5 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/error/AdviceController.java @@ -0,0 +1,42 @@ +package com.example.ipLab.StoreDataBase.util.error; + +import com.example.ipLab.StoreDataBase.Exceptions.CustomerNotFoundException; +import com.example.ipLab.StoreDataBase.Exceptions.OrderedNotFoundException; +import com.example.ipLab.StoreDataBase.Exceptions.ProductNotFoundException; +import com.example.ipLab.StoreDataBase.Exceptions.StoreNotFoundException; +import com.example.ipLab.StoreDataBase.util.validation.ValidationException; +import org.springframework.context.support.DefaultMessageSourceResolvable; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +import java.util.stream.Collectors; + +@ControllerAdvice +public class AdviceController { + @ExceptionHandler({ + CustomerNotFoundException.class, + OrderedNotFoundException.class, + ProductNotFoundException.class, + StoreNotFoundException.class + }) + public ResponseEntity handleException(Throwable e){ + return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); + } + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleBindException(MethodArgumentNotValidException e) { + final ValidationException validationException = new ValidationException( + e.getBindingResult().getAllErrors().stream() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .collect(Collectors.toSet())); + return handleException(validationException); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleUnknownException(Throwable e) { + e.printStackTrace(); + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidationException.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidationException.java new file mode 100644 index 0000000..41bbcac --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidationException.java @@ -0,0 +1,9 @@ +package com.example.ipLab.StoreDataBase.util.validation; + +import java.util.Set; + +public class ValidationException extends RuntimeException{ + public ValidationException(Set errors){ + super(String.join("\n", errors)); + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidatorUtil.java b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidatorUtil.java new file mode 100644 index 0000000..2d9bb68 --- /dev/null +++ b/backend/ipLab/src/main/java/com/example/ipLab/StoreDataBase/util/validation/ValidatorUtil.java @@ -0,0 +1,30 @@ +package com.example.ipLab.StoreDataBase.util.validation; + +import jakarta.validation.ConstraintViolation; +import jakarta.validation.Validation; +import jakarta.validation.Validator; +import jakarta.validation.ValidatorFactory; +import org.springframework.stereotype.Component; + +import java.util.Set; +import java.util.stream.Collectors; + +@Component +public class ValidatorUtil { + private final Validator validator; + + public ValidatorUtil(){ + try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()){ + this.validator = factory.getValidator(); + } + } + + public void validate(T object) { + final Set> errors = validator.validate(object); + if (!errors.isEmpty()) { + throw new ValidationException(errors.stream() + .map(ConstraintViolation::getMessage) + .collect(Collectors.toSet())); + } + } +} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java deleted file mode 100644 index d0b8da2..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Configuration/CalcConfiguration.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.ipLab.TypesCalc.Configuration; - -import com.example.ipLab.TypesCalc.domen.TypeCalcInteger; -import com.example.ipLab.TypesCalc.domen.TypeCalcString; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class CalcConfiguration { - private final Logger log = LoggerFactory.getLogger(CalcConfiguration.class); - - @Bean(value="int") - public TypeCalcInteger createTypeCalcInteger(){ - return new TypeCalcInteger(); - } - - @Bean(value="string") - public TypeCalcString createTypeCalcString(){ - return new TypeCalcString(); - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java deleted file mode 100644 index 306cfc2..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/Service/CalcService.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.example.ipLab.TypesCalc.Service; - -import com.example.ipLab.TypesCalc.domen.TypeCalc; -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Service; - -@Service -public class CalcService { - private final ApplicationContext applicationContext; - - public CalcService(ApplicationContext applicationContext){this.applicationContext = applicationContext;} - - public Object Sum(Object obj1, Object obj2, String type){ - final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); - if (type.startsWith("int")) return typeCalculator.Sum(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); - return typeCalculator.Sum(obj1, obj2); - } - public Object Dif(Object obj1, Object obj2, String type){ - final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); - if (type.startsWith("int")) return typeCalculator.Dif(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); - return typeCalculator.Dif(obj1, obj2); - } - public Object Multiply(Object obj1, Object obj2, String type){ - final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); - if (type.startsWith("int")) return typeCalculator.Multiply(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); - return typeCalculator.Multiply(obj1, obj2); - } - public Object Div(Object obj1, Object obj2, String type){ - final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type); - if (type.startsWith("int")) return typeCalculator.Div(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString())); - return typeCalculator.Div(obj1, obj2); - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java deleted file mode 100644 index 55decec..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/controller/TypeCalcController.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.example.ipLab.TypesCalc.controller; - -import com.example.ipLab.TypesCalc.Service.CalcService; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class TypeCalcController { - private final CalcService calcService; - - public TypeCalcController(CalcService calcService) {this.calcService = calcService;} - - @GetMapping("/CalcSum") - public Object calcSum(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, - @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, - @RequestParam(value = "type", defaultValue = "string") String type){ - return calcService.Sum(obj1, obj2, type); - } - - @GetMapping("/CalcDif") - public Object calcDif(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, - @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, - @RequestParam(value = "type", defaultValue = "string") String type){ - return calcService.Dif(obj1, obj2, type); - } - - @GetMapping("/CalcMultiply") - public Object calcMultiply(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, - @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, - @RequestParam(value = "type", defaultValue = "string") String type){ - return calcService.Multiply(obj1, obj2, type); - } - - @GetMapping("/CalcDiv") - public Object calcDiv(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1, - @RequestParam(value = "obj2", defaultValue = "provided") Object obj2, - @RequestParam(value = "type", defaultValue = "string") String type){ - return calcService.Div(obj1, obj2, type); - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java deleted file mode 100644 index b5f4038..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalc.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.ipLab.TypesCalc.domen; - -public interface TypeCalc { - T Sum(T obj1, T obj2); - T Dif(T obj1, T obj2); - T Multiply(T obj1, T obj2); - T Div(T obj1, T obj2); -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java deleted file mode 100644 index 4fe348d..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcInteger.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.example.ipLab.TypesCalc.domen; - -import org.springframework.stereotype.Component; - -public class TypeCalcInteger implements TypeCalc{ - @Override - public Integer Sum(Integer num1, Integer num2) { - return num1 + num2; - } - - @Override - public Integer Dif(Integer num1, Integer num2) { - return num1 - num2; - } - - @Override - public Integer Multiply(Integer num1, Integer num2) { - return num1 * num2; - } - - @Override - public Integer Div(Integer num1, Integer num2) { - try { - return num1 / num2; - } - catch (ArithmeticException ex){ - return 0; - } - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java b/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java deleted file mode 100644 index 969cecb..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/TypesCalc/domen/TypeCalcString.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.example.ipLab.TypesCalc.domen; - -import org.springframework.stereotype.Component; - -public class TypeCalcString implements TypeCalc { - @Override - public String Sum(String s1, String s2) { - return s1 + s2; - } - - @Override - public String Dif(String s1, String s2) { - String res = ""; - for (char c : s1.toCharArray()){ - boolean foundInOther = false; - for (int i = 0; i < s2.length(); i++){ - if (c == s2.charAt(i)) { - foundInOther = true; - break; - } - } - if (!foundInOther) res += c; - } - return res; - } - - @Override - public String Multiply(String s1, String s2) { - String res = ""; - for (char c : s1.toCharArray()){ - boolean foundInOther = false; - for (int i = 0; i < s2.length(); i++){ - if (c == s2.charAt(i)) { - foundInOther = true; - break; - } - } - if (foundInOther) res += c; - } - return res; - } - - @Override - public String Div(String s1, String s2) { - StringBuilder res = new StringBuilder(); - int maxLength = Integer.max(s1.length(), s2.length()); - for (int i = 0; i < maxLength; i++){ - if (i < s1.length()){ - res.append(s1.charAt(i)); - } - if (i < s2.length()){ - res.append(s2.charAt(i)); - } - } - return res.toString(); - } -} diff --git a/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java b/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java deleted file mode 100644 index 9c624de..0000000 --- a/backend/ipLab/src/main/java/com/example/ipLab/controllers/CalcController.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.example.ipLab.controllers; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class CalcController { - - @GetMapping("/second") - public int second(@RequestParam(value = "num") int num){ - return num*num; - } - - @GetMapping("/root") - public double root(@RequestParam(value = "num") int num){ - return Math.sqrt(num); - } - - @GetMapping("/fact") - public int fact(@RequestParam(value = "num") int num){ - int res = 1; - for (int i = 2; i <= num; i++) { - res *= i; - } - return res; - } - - @GetMapping("/digit") - public int digit(@RequestParam(value = "num") int num){ - if (num < 0) num *= -1; - int sum = 0; - while (num > 0) { - sum += num % 10; - num /= 10; - } - return sum; - } -}