3 лаб. работа.
This commit is contained in:
parent
1a08548c91
commit
3878d6101a
14
build.gradle
14
build.gradle
@ -2,6 +2,7 @@ plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.0.2'
|
||||
id 'io.spring.dependency-management' version '1.1.0'
|
||||
|
||||
}
|
||||
|
||||
group = 'com.lab'
|
||||
@ -14,10 +15,15 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testImplementation 'junit:junit:4.13.1'
|
||||
testImplementation 'junit:junit:4.13.1'
|
||||
testImplementation 'org.testng:testng:7.1.0'
|
||||
implementation 'com.h2database:h2:2.1.214'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.15'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.0.4'
|
||||
implementation 'org.hibernate:hibernate-core:6.1.7.Final'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
testImplementation 'org.testng:testng:7.7.0'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
|
@ -0,0 +1,29 @@
|
||||
package com.lab.springbapp.configuration;
|
||||
|
||||
import com.lab.springbapp.domain.TypeArray;
|
||||
import com.lab.springbapp.domain.TypeInt;
|
||||
import com.lab.springbapp.domain.TypeString;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Configuration
|
||||
public class TypeConfiguration {
|
||||
|
||||
//@Bean(value = "int")
|
||||
public TypeInt createCalculationInt(){
|
||||
return new TypeInt();
|
||||
}
|
||||
|
||||
//@Bean(value = "str")
|
||||
public TypeString createCalculationStr() {
|
||||
return new TypeString();
|
||||
}
|
||||
|
||||
//@Bean (value = "arr")
|
||||
public TypeArray createCalculationArr() {
|
||||
return new TypeArray();
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.lab.springbapp.controller;
|
||||
|
||||
import com.lab.springbapp.service.TypeService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class LabController {
|
||||
private final TypeService Service;
|
||||
|
||||
public LabController(TypeService service) {
|
||||
this.Service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/Sum")
|
||||
public Object Sum(@RequestParam(value = "obj1", defaultValue = "null") Object obj1,
|
||||
@RequestParam(value = "obj2", defaultValue = "null") Object obj2,
|
||||
@RequestParam(value = "typeInp", defaultValue = "str") String typeInp) {
|
||||
return Service.Sum(obj1, obj2, typeInp);
|
||||
}
|
||||
|
||||
@GetMapping("/Min")
|
||||
public Object Min(@RequestParam(value = "obj1", defaultValue = "null") Object obj1,
|
||||
@RequestParam(value = "obj2", defaultValue = "null") Object obj2,
|
||||
@RequestParam(value = "typeInp", defaultValue = "str") String typeInp) {
|
||||
return Service.Min(obj1, obj2, typeInp);
|
||||
}
|
||||
|
||||
@GetMapping("/Mul")
|
||||
public Object Mul(@RequestParam(value = "obj1", defaultValue = "null") Object obj1,
|
||||
@RequestParam(value = "obj2", defaultValue = "null") Object obj2,
|
||||
@RequestParam(value = "typeInp", defaultValue = "str") String typeInp) {
|
||||
return Service.Mul(obj1, obj2, typeInp);
|
||||
}
|
||||
|
||||
@GetMapping("/Del")
|
||||
public Object Del(@RequestParam(value = "obj1", defaultValue = "null") Object obj1,
|
||||
@RequestParam(value = "obj2", defaultValue = "null") Object obj2,
|
||||
@RequestParam(value = "typeInp", defaultValue = "str") String typeInp) {
|
||||
return Service.Del(obj1, obj2, typeInp);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.lab.springbapp.domain;
|
||||
|
||||
public interface ITypeInterface<T> {
|
||||
T Sum(T value1, T value2);
|
||||
T Min(T value1, T value2);
|
||||
T Mul(T value1, T value2);
|
||||
T Del(T value1, T value2);
|
||||
}
|
93
src/main/java/com/lab/springbapp/domain/TypeArray.java
Normal file
93
src/main/java/com/lab/springbapp/domain/TypeArray.java
Normal file
@ -0,0 +1,93 @@
|
||||
package com.lab.springbapp.domain;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "arr")
|
||||
public class TypeArray implements ITypeInterface<String[]> {
|
||||
@Override
|
||||
public String[] Sum(String[] value1, String[] value2) {
|
||||
String [] tempArr1;
|
||||
String [] tempArr2;
|
||||
if (value1.length > value2.length){
|
||||
tempArr1 = value1.clone();
|
||||
tempArr2 = value2.clone();
|
||||
}
|
||||
else{
|
||||
tempArr1 = value2.clone();
|
||||
tempArr2 = value1.clone();
|
||||
}
|
||||
String [] ans = new String[tempArr1.length];
|
||||
for (int i = 0; i < tempArr2.length; i ++){
|
||||
ans[i] = Integer.toString(Integer.parseInt(tempArr1[i]) + Integer.parseInt(tempArr2[i]));
|
||||
}
|
||||
for (int i = tempArr2.length; i < tempArr1.length; i++){
|
||||
ans[i] = tempArr1[i];
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] Min(String[] value1, String[] value2) {
|
||||
String [] tempArr1;
|
||||
String [] tempArr2;
|
||||
if (value1.length > value2.length){
|
||||
tempArr1 = value1.clone();
|
||||
tempArr2 = value2.clone();
|
||||
}
|
||||
else{
|
||||
tempArr1 = value2.clone();
|
||||
tempArr2 = value1.clone();
|
||||
}
|
||||
String [] ans = new String[tempArr1.length];
|
||||
for (int i = 0; i < tempArr2.length; i++){
|
||||
ans[i] = Integer.toString(Integer.parseInt(tempArr1[i]) - Integer.parseInt(tempArr2[i]));
|
||||
}
|
||||
for (int i = tempArr2.length; i < tempArr1.length; i++){
|
||||
ans[i] = tempArr1[i];
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] Mul(String[] value1, String[] value2) {
|
||||
String [] tempArr1;
|
||||
String [] tempArr2;
|
||||
if (value1.length > value2.length){
|
||||
tempArr1 = value1.clone();
|
||||
tempArr2 = value2.clone();
|
||||
}
|
||||
else{
|
||||
tempArr1 = value2.clone();
|
||||
tempArr2 = value1.clone();
|
||||
}
|
||||
String [] ans = new String[tempArr1.length];
|
||||
for (int i = 0; i < tempArr2.length; i ++){
|
||||
ans[i] = Integer.toString(Integer.parseInt(tempArr1[i]) * Integer.parseInt(tempArr2[i]));
|
||||
}
|
||||
for (int i = tempArr2.length; i < tempArr1.length; i++){
|
||||
ans[i] = tempArr1[i];
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] Del(String[] value1, String[] value2) {
|
||||
String [] tempArr1;
|
||||
String [] tempArr2;
|
||||
if (value1.length > value2.length){
|
||||
tempArr1 = value1.clone();
|
||||
tempArr2 = value2.clone();
|
||||
}
|
||||
else{
|
||||
tempArr1 = value2.clone();
|
||||
tempArr2 = value1.clone();
|
||||
}
|
||||
String [] ans = new String[tempArr1.length];
|
||||
for (int i = 0; i < tempArr2.length; i ++){
|
||||
ans[i] = Integer.toString(Integer.parseInt(tempArr1[i]) / Integer.parseInt(tempArr2[i]));
|
||||
}
|
||||
for (int i = tempArr2.length; i < tempArr1.length; i++){
|
||||
ans[i] = tempArr1[i];
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
31
src/main/java/com/lab/springbapp/domain/TypeInt.java
Normal file
31
src/main/java/com/lab/springbapp/domain/TypeInt.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.lab.springbapp.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "int")
|
||||
public class TypeInt implements ITypeInterface<Integer>{
|
||||
@Override
|
||||
public Integer Sum(Integer value1, Integer value2) {
|
||||
return value1 + value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer Min(Integer value1, Integer value2) {
|
||||
return value1 - value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer Mul(Integer value1, Integer value2) {
|
||||
return value1 * value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer Del(Integer value1, Integer value2) {
|
||||
if (value2 == 0){
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
return (value1/value2);
|
||||
}
|
||||
}
|
||||
}
|
51
src/main/java/com/lab/springbapp/domain/TypeString.java
Normal file
51
src/main/java/com/lab/springbapp/domain/TypeString.java
Normal file
@ -0,0 +1,51 @@
|
||||
package com.lab.springbapp.domain;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "str")
|
||||
public class TypeString implements ITypeInterface<String>{
|
||||
@Override
|
||||
public String Sum(String value1, String value2) {
|
||||
return value1 + value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Min(String value1, String value2) {
|
||||
StringBuilder bufStr = new StringBuilder();
|
||||
if (value1.equals(value2)){
|
||||
return "0";
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < value1.length(); i++) {
|
||||
boolean check = true;
|
||||
for (int k = 0; k < value2.length(); k++) {
|
||||
if (value1.charAt(i) == value2.charAt(k)) {
|
||||
check = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (check){
|
||||
bufStr.append(value1.charAt(i));}
|
||||
}
|
||||
}
|
||||
return bufStr.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Mul(String value1, String value2) {
|
||||
int multInt = value2.length();
|
||||
StringBuilder multResult = new StringBuilder();
|
||||
for (int i = 0; i < multInt; i++){
|
||||
multResult.append(value1);
|
||||
}
|
||||
return multResult.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Del(String value1, String value2) {
|
||||
if (value2.length() !=0) {
|
||||
double divRes = 1.0 * value1.length() / value2.length();
|
||||
return Double.toString(divRes);
|
||||
}
|
||||
else return "error";
|
||||
}
|
||||
}
|
75
src/main/java/com/lab/springbapp/service/TypeService.java
Normal file
75
src/main/java/com/lab/springbapp/service/TypeService.java
Normal file
@ -0,0 +1,75 @@
|
||||
package com.lab.springbapp.service;
|
||||
|
||||
import com.lab.springbapp.domain.ITypeInterface;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Service
|
||||
public class TypeService {
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public TypeService(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
|
||||
public Object Sum(Object value1, Object value2, String type) {
|
||||
final ITypeInterface calculator = (ITypeInterface) applicationContext.getBean(type);
|
||||
if(type.equals("str")){
|
||||
return calculator.Sum(value1.toString(),value2.toString());
|
||||
}
|
||||
if(type.equals("int")) {
|
||||
return calculator.Sum(Integer.parseInt(value1.toString()), Integer.parseInt(value2.toString()) );
|
||||
}
|
||||
if(type.equals("arr")){
|
||||
return calculator.Sum(cleanToArr(value1) , cleanToArr(value2));
|
||||
}
|
||||
return "error";
|
||||
}
|
||||
public Object Min(Object value1, Object value2, String type) {
|
||||
final ITypeInterface calculator = (ITypeInterface) applicationContext.getBean(type);
|
||||
if(type.equals("str")){
|
||||
return calculator.Min(value1.toString(),value2.toString());
|
||||
}
|
||||
if (type.equals("int")){
|
||||
return calculator.Min(Integer.parseInt(value1.toString()), Integer.parseInt(value2.toString()));
|
||||
}
|
||||
if(type.equals("arr")){
|
||||
return calculator.Min(cleanToArr(value1) , cleanToArr(value2));
|
||||
}
|
||||
return "error";
|
||||
}
|
||||
public Object Mul(Object value1, Object value2, String type) {
|
||||
final ITypeInterface calculator = (ITypeInterface) applicationContext.getBean(type);
|
||||
if(type.equals("str")){
|
||||
return calculator.Mul(value1.toString(), value2.toString());
|
||||
}
|
||||
if (type.equals("int")){
|
||||
return calculator.Mul(Integer.parseInt(value1.toString()), Integer.parseInt(value2.toString()));
|
||||
}
|
||||
if (type.equals("arr")){
|
||||
return calculator.Mul(cleanToArr(value1) , cleanToArr(value2));
|
||||
}
|
||||
return "error";
|
||||
}
|
||||
public Object Del(Object value1, Object value2, String type) {
|
||||
final ITypeInterface calculator = (ITypeInterface) applicationContext.getBean(type);
|
||||
if (type.equals("str")){
|
||||
return calculator.Del(value1.toString(), value2.toString());
|
||||
}
|
||||
if (type.equals("int")){
|
||||
return calculator.Del(Integer.parseInt(value1.toString()), Integer.parseInt(value2.toString()));
|
||||
}
|
||||
if(type.equals("arr")){
|
||||
return calculator.Del(cleanToArr(value1) , cleanToArr(value2));
|
||||
}
|
||||
return "error";
|
||||
}
|
||||
|
||||
public String[] cleanToArr(Object arr){
|
||||
return Arrays.stream(arr.toString().split("[\\(\\)\\,\\.\\[\\] \\!\\?\\:\\;]")).filter(e -> e.trim().length()>0).toArray(String[]::new);
|
||||
}
|
||||
}
|
77
src/main/java/database/model/Category.java
Normal file
77
src/main/java/database/model/Category.java
Normal file
@ -0,0 +1,77 @@
|
||||
package database.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "playlist")
|
||||
public class Category {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
private String categoryName;
|
||||
@ManyToMany(fetch = FetchType.EAGER,mappedBy = "categories")
|
||||
private List<Video> videos;
|
||||
|
||||
public Category(){}
|
||||
|
||||
public Category(String _categoryName){
|
||||
categoryName = _categoryName;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getCategoryName(){
|
||||
return categoryName;
|
||||
}
|
||||
|
||||
public void setCategoryName(String _categoryName){
|
||||
categoryName = _categoryName;
|
||||
}
|
||||
|
||||
public List<Video> getVideos(){
|
||||
return videos;
|
||||
}
|
||||
public void addVideo(Video video){
|
||||
if(videos==null) {
|
||||
videos = new ArrayList<>();
|
||||
}
|
||||
videos.add(video);
|
||||
}
|
||||
public void removeVideo(Video video){
|
||||
if(video!=null)
|
||||
videos.remove(video);
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Category category = (Category) o;
|
||||
if(!this.categoryName.equals(category.getCategoryName())){
|
||||
return false;
|
||||
}
|
||||
if(this.id != category.getId()) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(id, category.id);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Category{" +
|
||||
"id=" + id +
|
||||
", categoryName='" + categoryName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
101
src/main/java/database/model/User.java
Normal file
101
src/main/java/database/model/User.java
Normal file
@ -0,0 +1,101 @@
|
||||
package database.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
|
||||
@Entity
|
||||
@Table(name = "user")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String userName;
|
||||
|
||||
@Column(name = "subscribers count")
|
||||
private int subsCount;
|
||||
|
||||
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
|
||||
private List<Video> videos;
|
||||
|
||||
public User(){ }
|
||||
|
||||
public User(String _userName, int _subsCount) {
|
||||
this.userName = _userName;
|
||||
this.subsCount = _subsCount;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String _userName) {
|
||||
userName = _userName;
|
||||
}
|
||||
|
||||
public int getSubsCount() {
|
||||
return subsCount;
|
||||
}
|
||||
|
||||
public void setSubsCount(int _subsCount) {
|
||||
subsCount += _subsCount;
|
||||
}
|
||||
|
||||
public List<Video> getVideos() {
|
||||
return videos;
|
||||
}
|
||||
|
||||
public void addVideo(Video video){
|
||||
if (videos == null){
|
||||
videos = new ArrayList<>();
|
||||
}
|
||||
videos.add(video);
|
||||
}
|
||||
|
||||
public void removeVideo(Video video){
|
||||
if (video != null){
|
||||
videos.remove(video);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
User user = (User) o;
|
||||
if(this.id != user.getId())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(this.userName.equals(user.getUserName()) ) {
|
||||
return false;
|
||||
}
|
||||
if(this.subsCount != user.getSubsCount()) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(id, user.id);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", userName='" + userName + '\'' +
|
||||
", subsCount: " + subsCount + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
142
src/main/java/database/model/Video.java
Normal file
142
src/main/java/database/model/Video.java
Normal file
@ -0,0 +1,142 @@
|
||||
package database.model;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "video")
|
||||
public class Video {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String videoName;
|
||||
|
||||
@Column(name = "duration")
|
||||
private Double duration;
|
||||
|
||||
@ManyToOne
|
||||
private User user;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name="videos_categories",
|
||||
joinColumns = @JoinColumn(name="video_id"),
|
||||
inverseJoinColumns = @JoinColumn(name="category_id")
|
||||
)
|
||||
private List<Category> categories;
|
||||
|
||||
public Video(){
|
||||
}
|
||||
|
||||
public Video(String videoName, Double duration){
|
||||
this.videoName = videoName;
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getVideoName(){
|
||||
return videoName;
|
||||
}
|
||||
|
||||
public Double getDuration(){
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setVideoName(String videoName){
|
||||
this.videoName = videoName;
|
||||
}
|
||||
|
||||
public void setDuration(Double duration){
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User _user){
|
||||
if(user == null){
|
||||
user = _user;
|
||||
user.addVideo(this);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Category> getCategories(){
|
||||
return categories;
|
||||
}
|
||||
public void addCategory(Category category){
|
||||
if(categories==null)
|
||||
categories=new ArrayList<>();
|
||||
categories.add(category);
|
||||
category.addVideo(this);
|
||||
}
|
||||
public void removeCategory(Category category){
|
||||
if(categories!=null){
|
||||
categories.remove(category);
|
||||
category.removeVideo(this);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Video video = (Video) o;
|
||||
if (this.id != video.getId()) {
|
||||
return false;
|
||||
}
|
||||
if (!this.videoName.equals(video.getVideoName())) {
|
||||
return false;
|
||||
}
|
||||
if (!this.duration.equals(video.getDuration())) {
|
||||
return false;
|
||||
}
|
||||
if(this.user.getId() != video.getUser().getId() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(categories.size() != video.getCategories().size()) {
|
||||
return false;
|
||||
}
|
||||
if(categories.size() > 0){
|
||||
for (Category cat:
|
||||
categories) {
|
||||
Boolean check = false;
|
||||
for (Category catVid:
|
||||
video.getCategories()) {
|
||||
if(cat.equals(catVid))
|
||||
check = true;
|
||||
}
|
||||
if(!check)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return Objects.equals(id, video.id);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Video{" +
|
||||
"id=" + id +
|
||||
", videoName='" + videoName + '\'' +
|
||||
", duration='" + duration + '\'' +
|
||||
", User: " + (user == null ? "[]" : user.toString()) + '\'' +
|
||||
"Categories:" + (categories == null ? "[]" : categories.toString()) +
|
||||
'}';
|
||||
}
|
||||
}
|
64
src/main/java/database/service/CategoryService.java
Normal file
64
src/main/java/database/service/CategoryService.java
Normal file
@ -0,0 +1,64 @@
|
||||
package database.service;
|
||||
|
||||
import database.model.Category;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Service
|
||||
public class CategoryService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Category addCategory(String categoryName) {
|
||||
if (!StringUtils.hasText(categoryName) ) {
|
||||
throw new IllegalArgumentException("Not good data");
|
||||
}
|
||||
final Category category = new Category(categoryName);
|
||||
em.persist(category);
|
||||
return category;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Category findCategory(Long id) {
|
||||
final Category category = em.find(Category.class, id);
|
||||
if (category == null) {
|
||||
throw new EntityNotFoundException(String.format("Category with id [%s] is not found", id));
|
||||
}
|
||||
return category;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Category> findAllCategories() {
|
||||
return em.createQuery("select c from Category c", Category.class).getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Category updateCategory(Long id, String categoryName) {
|
||||
if (!StringUtils.hasText(categoryName) ) {
|
||||
throw new IllegalArgumentException("Not good data");
|
||||
}
|
||||
final Category currentCategory = findCategory(id);
|
||||
currentCategory.setCategoryName(categoryName);
|
||||
return em.merge(currentCategory);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Category deleteCategory(Long id) {
|
||||
final Category currentCategory = findCategory(id);
|
||||
em.remove(currentCategory);
|
||||
return currentCategory;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllCategories() {
|
||||
em.createQuery("delete from Category").executeUpdate();
|
||||
}
|
||||
}
|
65
src/main/java/database/service/UserService.java
Normal file
65
src/main/java/database/service/UserService.java
Normal file
@ -0,0 +1,65 @@
|
||||
package database.service;
|
||||
|
||||
import database.model.User;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public User addUser(String userName, int subsCount) {
|
||||
if (!StringUtils.hasText(userName) || subsCount <0 ) {
|
||||
throw new IllegalArgumentException("Not good data");
|
||||
}
|
||||
final User user = new User(userName,subsCount);
|
||||
em.persist(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public User findUser(Long id) {
|
||||
final User user = em.find(User.class, id);
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<User> findAllUsers() {
|
||||
return em.createQuery("select b from User b", User.class).getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public User updateUser(Long id, String userName, int subsCount) {
|
||||
if (!StringUtils.hasText(userName) || subsCount <0) {
|
||||
throw new IllegalArgumentException("Not good data");
|
||||
}
|
||||
final User currentUser = findUser(id);
|
||||
currentUser.setUserName(userName);
|
||||
currentUser.setSubsCount(subsCount);
|
||||
return em.merge(currentUser);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public User deleteUser(Long id) {
|
||||
final User currentUser = findUser(id);
|
||||
em.remove(currentUser);
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllUsers() {
|
||||
em.createQuery("delete from User").executeUpdate();
|
||||
}
|
||||
}
|
118
src/main/java/database/service/VideoService.java
Normal file
118
src/main/java/database/service/VideoService.java
Normal file
@ -0,0 +1,118 @@
|
||||
package database.service;
|
||||
|
||||
import database.model.User;
|
||||
import database.model.Video;
|
||||
import database.model.Category;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class VideoService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Video addVideo(String videoName, double duration) throws IOException {
|
||||
if (!StringUtils.hasText(videoName) || duration <= 0) {
|
||||
throw new IllegalArgumentException("Not good data");
|
||||
}
|
||||
final Video video = new Video(videoName, duration);
|
||||
em.persist(video);
|
||||
return video;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Video addUser(Long VideoId, Long UserId) {
|
||||
final Video video = em.find(Video.class, VideoId);
|
||||
if (video == null) {
|
||||
throw new EntityNotFoundException(String.format("Video with id [%s] is not found", VideoId));
|
||||
}
|
||||
|
||||
final User user = em.find(User.class, UserId);
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException(String.format("User with id [%s] is not found", UserId));
|
||||
}
|
||||
|
||||
video.setUser(user);
|
||||
em.merge(video);
|
||||
em.merge(user);
|
||||
return video;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Video addCategory(Long VideoId, Long CategoryId) {
|
||||
final Video video = em.find(Video.class, VideoId);
|
||||
if (video == null) {
|
||||
throw new EntityNotFoundException(String.format("Video with id [%s] is not found", VideoId));
|
||||
}
|
||||
|
||||
final Category category = em.find(Category.class, CategoryId);
|
||||
if (category == null) {
|
||||
throw new EntityNotFoundException(String.format("Category with id [%s] is not found", CategoryId));
|
||||
}
|
||||
video.addCategory(category);
|
||||
return em.merge(video);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Video removeCategory(Long VideoId, Long CategoryId) {
|
||||
final Video video = em.find(Video.class, VideoId);
|
||||
final Category category = em.find(Category.class, CategoryId);
|
||||
if (CategoryId <= 0 || category == null) {
|
||||
throw new IllegalArgumentException("Category with id [%s] is not found");
|
||||
}
|
||||
video.removeCategory(category);
|
||||
return em.merge(video);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Video findVideo(Long id) {
|
||||
final Video video = em.find(Video.class, id);
|
||||
if (video == null) {
|
||||
throw new EntityNotFoundException(String.format("Video with id [%s] is not found", id));
|
||||
}
|
||||
return video;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Video> findAllVideos() {
|
||||
return em.createQuery("select v from Video v", Video.class).getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Video updateVideo(Long id, String videoName) {
|
||||
if (!StringUtils.hasText(videoName)) {
|
||||
throw new IllegalArgumentException("Not good data");
|
||||
}
|
||||
final Video currentVideo = findVideo(id);
|
||||
currentVideo.setVideoName(videoName);
|
||||
return em.merge(currentVideo);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Video deleteVideo(Long id) {
|
||||
final Video currentVideo = findVideo(id);
|
||||
em.remove(currentVideo);
|
||||
return currentVideo;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllVideos() {
|
||||
em.createQuery("delete from Video").executeUpdate();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Video> findVideoWithDur(double from, double to) {
|
||||
return em.createQuery("select v from Video v where v.duration between :from and :to", Video.class)
|
||||
.setParameter("from", from)
|
||||
.setParameter("to", to)
|
||||
.getResultList();
|
||||
}
|
||||
}
|
64
src/test/java/com/lab/springbapp/VideoTest.java
Normal file
64
src/test/java/com/lab/springbapp/VideoTest.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.lab.springbapp;
|
||||
|
||||
import database.model.Category;
|
||||
import database.model.User;
|
||||
import database.model.Video;
|
||||
import database.service.CategoryService;
|
||||
import database.service.UserService;
|
||||
import database.service.VideoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@SpringBootTest
|
||||
public class VideoTest {
|
||||
private static final Logger log = LoggerFactory.getLogger(VideoTest.class);
|
||||
|
||||
@Autowired
|
||||
private VideoService videoService;
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
public VideoTest(VideoService videoService) {
|
||||
this.videoService = videoService;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testVideoFindDur() {
|
||||
videoService.deleteAllVideos();
|
||||
categoryService.deleteAllCategories();
|
||||
userService.deleteAllUsers();
|
||||
final Category cat1 = categoryService.addCategory("Music");
|
||||
final User user2 = userService.addUser("newUser",10000);
|
||||
final Video video1;
|
||||
try {
|
||||
video1 = videoService.addVideo("newVideo1",30);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
final Video video2;
|
||||
try {
|
||||
video2 = videoService.addVideo("newVideo2",10);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
video1.addCategory(cat1);
|
||||
video2.addCategory(cat1);
|
||||
video1.setUser(user2);
|
||||
video2.setUser(user2);
|
||||
final List<Video> videos = videoService.findVideoWithDur(5,15);
|
||||
log.info("testBloggerCount" + videos.size());
|
||||
Assertions.assertEquals(videos.size(),1);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user