Skip to content

Commit

Permalink
Adding school, repo, service and annotation for it
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoleNG18 committed Apr 8, 2024
1 parent 37bb580 commit cbbc218
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pmgkn.pescores.pescores.domain.dto.binding;

import jakarta.validation.constraints.NotEmpty;
import pmgkn.pescores.pescores.validation.common.UniqueSchoolName;

import static pmgkn.pescores.pescores.domain.entity.constants.ValidationErrorMessages.*;

public class SchoolAddBindingDto {

@NotEmpty(message = SCHOOL_NAME_REQUIRED)
@UniqueSchoolName
private String name;

@NotEmpty(message = CITY_NAME_REQUIRED)
private String city;

public SchoolAddBindingDto() {
}

public String getName() {
return name;
}

public SchoolAddBindingDto setName(String name) {
this.name = name;
return this;
}

public String getCity() {
return city;
}

public SchoolAddBindingDto setCity(String city) {
this.city = city;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package pmgkn.pescores.pescores.domain.entity;

import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;

@Table(name = "schools")
@Entity
public class SchoolEntity extends BaseEntity {

@Column(nullable = false, unique = true)
private String name;

@Column(nullable = false)
private String city;

@OneToMany(cascade = CascadeType.ALL)
private List<ClassEntity> classes;

@OneToMany(cascade = CascadeType.ALL)
private List<StudentEntity> students;

@OneToMany(cascade = CascadeType.ALL)
private List<UserEntity> users;

public SchoolEntity() {
this.classes = new ArrayList<>();
this.users = new ArrayList<>();
this.students = new ArrayList<>();
}

public String getName() {
return name;
}

public SchoolEntity setName(String name) {
this.name = name;
return this;
}

public String getCity() {
return city;
}

public SchoolEntity setCity(String city) {
this.city = city;
return this;
}

public List<ClassEntity> getClasses() {
return classes;
}

public SchoolEntity setClasses(List<ClassEntity> classes) {
this.classes = classes;
return this;
}

public List<StudentEntity> getStudents() {
return students;
}

public SchoolEntity setStudents(List<StudentEntity> students) {
this.students = students;
return this;
}

public List<UserEntity> getUsers() {
return users;
}

public SchoolEntity setUsers(List<UserEntity> users) {
this.users = users;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public enum ValidationErrorMessages {
public static final String NOT_EMPTY_NUMBER = "number must not be empty";
public static final String DESCRIPTION_REQUIRED = "description is required";
public static final String CLASS_NAME_REQUIRED = "class name is required";
public static final String CITY_NAME_REQUIRED = "city name is required";
public static final String SCHOOL_NAME_REQUIRED = "school name is required";
public static final String CLASS_NUM_REQUIRED = "class number is required";
public static final String DATE_PRESENT = "use present date";
public static final String MATCHING_PASSWORDS = "Passwords should match.";
Expand All @@ -14,6 +16,7 @@ public enum ValidationErrorMessages {
public static final String INVALID_EMAIL = "Email should be valid";
public static final String INVALID_TASK = "Should be up to 35 symbols";
public static final String INVALID_CASS_NAME = "Class name should have grade number";
public static final String UNIQUE_SCHOOL_NAME = "school name should be unique";
public static final String INVALID_SCHOOL_NAME = "At least 15 symbols.";
public static final String FIELDS_MATCH = "Fields should match";
public static final String UNIQUE_EMAIL = "Email should be unique.";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package pmgkn.pescores.pescores.domain.enums;

public enum UserRoleEnum {
USER, ADMIN
USER, ADMIN, SUPERADMIN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pmgkn.pescores.pescores.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import pmgkn.pescores.pescores.domain.entity.SchoolEntity;

import java.util.Optional;
import java.util.UUID;

@Repository
public interface SchoolRepository extends JpaRepository<SchoolEntity, UUID> {

Optional<SchoolEntity> findByName(String name);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pmgkn.pescores.pescores.service;

import org.springframework.stereotype.Service;

@Service
public class SchoolService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pmgkn.pescores.pescores.validation.common;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static pmgkn.pescores.pescores.domain.entity.constants.ValidationErrorMessages.UNIQUE_SCHOOL_NAME;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Constraint(validatedBy = UniqueSchoolNameValidator.class)
public @interface UniqueSchoolName {

String message() default UNIQUE_SCHOOL_NAME;

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pmgkn.pescores.pescores.validation.common;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import pmgkn.pescores.pescores.repositories.SchoolRepository;

public class UniqueSchoolNameValidator implements ConstraintValidator<UniqueSchoolName, String> {

private final SchoolRepository schoolRepository;

public UniqueSchoolNameValidator(SchoolRepository schoolRepository) {
this.schoolRepository = schoolRepository;
}

@Override
public boolean isValid(String schoolName,
ConstraintValidatorContext context) {

return this.schoolRepository
.findByName(schoolName)
.isEmpty();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
password: ${DB_PASSWORD}
url: jdbc:mysql://scores-pe-db.mysql.database.azure.com:3306/pe_scores?serverTimezone=UTC&useSSL=true&requireSSL=false
# url: jdbc:mysql://localhost:3306/pe_scores?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true&serverTimezone=UTC
# url: jdbc:mysql://scores-pe-db.mysql.database.azure.com:3306/pe_scores?serverTimezone=UTC&useSSL=true&requireSSL=false
url: jdbc:mysql://localhost:3306/pe_scores?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true&serverTimezone=UTC
username: ${DB_USERNAME}
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
Expand Down

0 comments on commit cbbc218

Please sign in to comment.