Skip to content

Commit

Permalink
Merge pull request #105 from Sexy-Sisters/feat/teacher
Browse files Browse the repository at this point in the history
Feat/teacher
  • Loading branch information
namse79 authored Dec 21, 2022
2 parents 55adc15 + 80063a5 commit 286de20
Show file tree
Hide file tree
Showing 31 changed files with 367 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ interface StudentMapper {

@Mappings(
value = [
Mapping(source = "user.profileImg", target = "profileImg"),
Mapping(source = "user.nickname", target = "nickname"),
Mapping(source = "user.image.value", target = "profileImg"),
Mapping(source = "user.nickname.value", target = "nickname"),
Mapping(source = "grade.value", target = "grade"),
Mapping(source = "classroom.value", target = "classroom"),
Mapping(source = "number.value", target = "number"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import javax.persistence.*
@Entity
@Table(name = "tbl_student")
class Student(
@Embedded val grade: Grade,
@Embedded val classroom: Classroom,
@Embedded val number: Number,
@Embedded val age: Age,
@Embedded private val grade: Grade,
@Embedded private val classroom: Classroom,
@Embedded private val number: Number,
@Embedded private val age: Age,
) : BaseTimeEntity() {

@Enumerated(EnumType.STRING)
Expand Down Expand Up @@ -62,4 +62,6 @@ fun Student.waiting() {

fun Student.engaged() {
status = Status.ENGAGED
}
}

fun Student.isAttendSchool() = school != null
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ class TeacherCommand {
val nickname: String,
val bio: String,
)

data class Update(
val image: String,
val name: String,
val nickname: String,
val bio: String,
)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sexysisters.tojserverv2.domain.teacher

import com.sexysisters.tojserverv2.domain.school.domain.School
import com.sexysisters.tojserverv2.domain.teacher.domain.Teacher

interface TeacherReader {
fun search(schoolCode: String): List<Teacher>
fun getTeacher(id: Long): Teacher
fun getTeacher(id: Long, school: School): Teacher
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sexysisters.tojserverv2.domain.teacher

import org.mapstruct.*

@Mapper(
componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
unmappedTargetPolicy = ReportingPolicy.ERROR,
)
interface TeacherResponseMapper
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import com.sexysisters.tojserverv2.domain.teacher.domain.Teacher

interface TeacherStore {
fun store(teacher: Teacher): Teacher
fun delete(teacher: Teacher)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ import javax.persistence.Embeddable
import javax.validation.constraints.NotNull

@Embeddable
class Bio private constructor(
class Bio(
@field:NotNull
@Column(name = "bio")
val value: String
) {
companion object {
private val REGEX = Regex("^{1,500}$")

fun of(value: String): Bio {
validate(value)
return Bio(value)
}

private fun validate(value: String) {
if (REGEX.matches(value)) throw TeacherException.TeacherNotValid()
}
init {
val MIN_LENGTH = 1
val MAX_LENGTH = 500
if (value.length !in MIN_LENGTH..MAX_LENGTH) throw TeacherException.TeacherNotValid()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ import javax.persistence.Embeddable
import javax.validation.constraints.NotNull

@Embeddable
class Image private constructor (
class Image(
@URL
@field:NotNull
@Column(name = "image")
val value: String
) {
companion object {
fun of(value: String): Image {
validate(value)
return Image(value)
}

private fun validate(value: String) {
if (value.isBlank()) throw TeacherException.TeacherNotValid()
}
init {
if (value.isBlank()) throw TeacherException.TeacherNotValid()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ import javax.persistence.Embeddable
import javax.validation.constraints.NotNull

@Embeddable
class Name private constructor (
class Name(
@field:NotNull
@Column(name = "name", unique = true)
val value: String
) {
companion object {
private val REGEX = Regex("^{1,100}$")

fun of(value: String): Name {
validate(value)
return Name(value)
}

private fun validate(value: String) {
if (REGEX.matches(value)) throw TeacherException.TeacherNotValid()
}
init {
val MIN_LENGTH = 1
val MAX_LENGTH = 50
if (value.length !in MIN_LENGTH..MAX_LENGTH) throw TeacherException.TeacherNotValid()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ import javax.persistence.Embeddable
import javax.validation.constraints.NotNull

@Embeddable
class Nickname private constructor(
class Nickname(
@field:NotNull
@Column(name = "nickname")
val value: String
) {
companion object {
private val REGEX = Regex("^{1,100}$")

fun of(value: String): Nickname {
validate(value)
return Nickname(value)
}

private fun validate(value: String) {
if (REGEX.matches(value)) throw TeacherException.TeacherNotValid()
}
init {
val MIN_LENGTH = 1
val MAX_LENGTH = 50
if (value.length !in MIN_LENGTH..MAX_LENGTH) throw TeacherException.TeacherNotValid()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import javax.persistence.*
@Table(name = "tbl_teacher")
class Teacher(
@Embedded
val image: Image,
var image: Image,
@Embedded
val name: Name,
var name: Name,
@Embedded
val nickname: Nickname,
var nickname: Nickname,
@Embedded
val bio: Bio,
var bio: Bio,
) : BaseTimeEntity() {

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -23,4 +23,11 @@ class Teacher(

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0L

fun update(image: String, name: String, nickname: String, bio: String) {
this.image = Image(image)
this.name = Name(name)
this.nickname = Nickname(nickname)
this.bio = Bio(bio)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ enum class TeacherErrorCode(
TEACHER_NOT_VALID("Teacher domain is not valid"),
DUPLICATE_NAME("Teacher name is duplicate"),
DUPLICATE_NICKNAME("Teacher nickname is duplicate"),
TEACHER_NOT_FOUND("Teacher not found"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ class TeacherException {
class TeacherNotValid : BaseException(TeacherErrorCode.TEACHER_NOT_VALID)
class DuplicateTeacherName : BaseException(TeacherErrorCode.DUPLICATE_NAME)
class DuplicateTeacherNickname : BaseException(TeacherErrorCode.DUPLICATE_NICKNAME)
class TeacherNotFound : BaseException(TeacherErrorCode.TEACHER_NOT_FOUND)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.sexysisters.tojserverv2.domain.teacher.service

import com.sexysisters.tojserverv2.domain.teacher.TeacherCommand
import com.sexysisters.tojserverv2.interfaces.teacher.dto.TeacherResponse

interface TeacherService {
fun createTeacher(command: TeacherCommand.Create)
fun create(command: TeacherCommand.Create)
fun getTeachers(schoolCode: String): List<TeacherResponse.Main>
fun getTeacher(id: Long): TeacherResponse.Main
fun update(id: Long, request: TeacherCommand.Update)
fun delete(id: Long)
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,69 @@
package com.sexysisters.tojserverv2.domain.teacher.service

import com.sexysisters.tojserverv2.domain.school.exception.SchoolException
import com.sexysisters.tojserverv2.domain.student.domain.Student
import com.sexysisters.tojserverv2.domain.student.domain.isAttendSchool
import com.sexysisters.tojserverv2.domain.student.exception.StudentException
import com.sexysisters.tojserverv2.domain.teacher.*
import com.sexysisters.tojserverv2.domain.teacher.domain.*
import com.sexysisters.tojserverv2.domain.user.UserReader
import com.sexysisters.tojserverv2.domain.user.hasStudent
import com.sexysisters.tojserverv2.interfaces.teacher.dto.TeacherDtoMapper
import com.sexysisters.tojserverv2.interfaces.teacher.dto.TeacherResponse
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
class TeacherServiceImpl(
private val userReader: UserReader,
private val teacherStore: TeacherStore,
private val teacherEntityMapper: TeacherEntityMapper,
private val teacherReader: TeacherReader,
private val teacherDtoMapper: TeacherDtoMapper,
) : TeacherService {

@Transactional
override fun createTeacher(command: TeacherCommand.Create) {
override fun create(command: TeacherCommand.Create) {
checkStudentIdentity()
val teacher = createTeacherEntity(command)
teacherStore.store(teacher)
}

@Transactional(readOnly = true)
override fun getTeachers(schoolCode: String): List<TeacherResponse.Main> {
val teachers = teacherReader.search(schoolCode)
return teachers.map { teacherDtoMapper.of(it) }
}

@Transactional(readOnly = true)
override fun getTeacher(id: Long): TeacherResponse.Main {
val teacher = teacherReader.getTeacher(id)
return teacherDtoMapper.of(teacher)
}

override fun update(id: Long, request: TeacherCommand.Update) {
val student = checkStudentIdentity()
if (!student.isAttendSchool()) throw SchoolException.SchoolNotFound()
val teacher = teacherReader.getTeacher(id, student.school!!)
teacher.update(request.image, request.name, request.nickname, request.bio)
}

override fun delete(id: Long) {
val student = checkStudentIdentity()
if (!student.isAttendSchool()) throw SchoolException.SchoolNotFound()
val teacher = teacherReader.getTeacher(id, student.school!!)
teacherStore.delete(teacher)
}

private fun createTeacherEntity(command: TeacherCommand.Create) = Teacher(
Image(command.image),
Name(command.name),
Nickname(command.nickname),
Bio(command.bio),
)

private fun checkStudentIdentity(): Student {
val user = userReader.getCurrentUser()
if (!user.hasStudent()) throw StudentException.StudentNotFound()
val teacher = teacherEntityMapper.of(command)
teacherStore.store(teacher)
return user.student!!
}
}
19 changes: 19 additions & 0 deletions src/main/kotlin/com/sexysisters/tojserverv2/domain/user/Email.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sexysisters.tojserverv2.domain.user

import com.sexysisters.tojserverv2.domain.user.exception.UserException
import javax.persistence.Column
import javax.persistence.Embeddable
import javax.validation.constraints.NotNull

@Embeddable
class Email(
@field:NotNull
@Column(name = "email")
private val value: String,
) {
init {
val EMAIL_FORMAT = "@"
if (value.isBlank()) throw UserException.UserNotValid()
if (!value.contains(EMAIL_FORMAT)) throw UserException.UserNotValid()
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/com/sexysisters/tojserverv2/domain/user/Image.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sexysisters.tojserverv2.domain.user

import com.sexysisters.tojserverv2.domain.user.exception.UserException
import javax.persistence.Column
import javax.persistence.Embeddable
import javax.validation.constraints.NotNull

@Embeddable
class Image(
@field:NotNull
@Column(name = "image")
private val value: String,
) {
init {
if (value.isBlank()) throw UserException.UserNotValid()
}
}
Loading

0 comments on commit 286de20

Please sign in to comment.