diff --git a/351002/Abrashin/.gitattributes b/351002/Abrashin/.gitattributes new file mode 100644 index 000000000..8af972cde --- /dev/null +++ b/351002/Abrashin/.gitattributes @@ -0,0 +1,3 @@ +/gradlew text eol=lf +*.bat text eol=crlf +*.jar binary diff --git a/351002/Abrashin/.gitignore b/351002/Abrashin/.gitignore new file mode 100644 index 000000000..cd70ec81d --- /dev/null +++ b/351002/Abrashin/.gitignore @@ -0,0 +1,39 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Kotlin ### +.kotlin diff --git a/351002/Abrashin/build.gradle.kts b/351002/Abrashin/build.gradle.kts new file mode 100644 index 000000000..0ed5f0f29 --- /dev/null +++ b/351002/Abrashin/build.gradle.kts @@ -0,0 +1,42 @@ +plugins { + kotlin("jvm") version "2.2.21" + kotlin("plugin.spring") version "2.2.21" + kotlin("kapt") version "2.2.21" + id("org.springframework.boot") version "3.4.3" + id("io.spring.dependency-management") version "1.1.7" +} + +group = "com.example" +version = "0.0.1-SNAPSHOT" +description = "DistComp" + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-validation") + implementation("org.jetbrains.kotlin:kotlin-reflect") + implementation("org.mapstruct:mapstruct:1.6.3") + kapt("org.mapstruct:mapstruct-processor:1.6.3") + testImplementation("org.springframework.boot:spring-boot-starter-test") + testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") +} + +kotlin { + compilerOptions { + freeCompilerArgs.addAll("-Xjsr305=strict", "-Xannotation-default-target=param-property") + } +} + +tasks.withType { + useJUnitPlatform() +} diff --git a/351002/Abrashin/gradle/wrapper/gradle-wrapper.jar b/351002/Abrashin/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..61285a659 Binary files /dev/null and b/351002/Abrashin/gradle/wrapper/gradle-wrapper.jar differ diff --git a/351002/Abrashin/gradle/wrapper/gradle-wrapper.properties b/351002/Abrashin/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 000000000..37f78a6af --- /dev/null +++ b/351002/Abrashin/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/351002/Abrashin/gradlew b/351002/Abrashin/gradlew new file mode 100644 index 000000000..adff685a0 --- /dev/null +++ b/351002/Abrashin/gradlew @@ -0,0 +1,248 @@ +#!/bin/sh + +# +# Copyright © 2015 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/351002/Abrashin/gradlew.bat b/351002/Abrashin/gradlew.bat new file mode 100644 index 000000000..c4bdd3ab8 --- /dev/null +++ b/351002/Abrashin/gradlew.bat @@ -0,0 +1,93 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/351002/Abrashin/settings.gradle.kts b/351002/Abrashin/settings.gradle.kts new file mode 100644 index 000000000..ac89496a4 --- /dev/null +++ b/351002/Abrashin/settings.gradle.kts @@ -0,0 +1,4 @@ +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0" +} +rootProject.name = "DistComp" diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/DistCompApplication.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/DistCompApplication.kt new file mode 100644 index 000000000..54b365a34 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/DistCompApplication.kt @@ -0,0 +1,11 @@ +package com.example.distcomp + +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + +@SpringBootApplication +class DistCompApplication + +fun main(args: Array) { + runApplication(*args) +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/config/JacksonConfig.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/config/JacksonConfig.kt new file mode 100644 index 000000000..239ae5d75 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/config/JacksonConfig.kt @@ -0,0 +1,16 @@ +package com.example.distcomp.config + +import com.fasterxml.jackson.databind.SerializationFeature +import org.springframework.context.annotation.Configuration +import org.springframework.http.converter.HttpMessageConverter +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer + +@Configuration +class JacksonConfig : WebMvcConfigurer { + override fun extendMessageConverters(converters: MutableList>) { + converters.filterIsInstance().forEach { + it.objectMapper.disable(SerializationFeature.WRAP_ROOT_VALUE) + } + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/CreatorController.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/CreatorController.kt new file mode 100644 index 000000000..6933168e2 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/CreatorController.kt @@ -0,0 +1,45 @@ +package com.example.distcomp.controller + +import com.example.distcomp.dto.request.CreatorRequestTo +import com.example.distcomp.dto.response.CreatorResponseTo +import com.example.distcomp.service.CreatorService +import jakarta.validation.Valid +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/api/v1.0/creators") +class CreatorController(private val service: CreatorService) { + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + fun create(@Valid @RequestBody request: CreatorRequestTo): CreatorResponseTo { + return service.create(request) + } + + @GetMapping("/{id}") + fun getById(@PathVariable id: Long): CreatorResponseTo { + return service.getById(id) + } + + @GetMapping + fun getAll(): List { + return service.getAll() + } + + @PutMapping("/{id}") + fun update(@PathVariable id: Long, @Valid @RequestBody request: CreatorRequestTo): CreatorResponseTo { + return service.patch(id, request) + } + + @PatchMapping("/{id}") + fun patch(@PathVariable id: Long, @Valid @RequestBody request: CreatorRequestTo): CreatorResponseTo { + return service.patch(id, request) + } + + @DeleteMapping("/{id}") + @ResponseStatus(HttpStatus.NO_CONTENT) + fun delete(@PathVariable id: Long) { + service.delete(id) + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/NoteController.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/NoteController.kt new file mode 100644 index 000000000..d31aa35fa --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/NoteController.kt @@ -0,0 +1,45 @@ +package com.example.distcomp.controller + +import com.example.distcomp.dto.request.NoteRequestTo +import com.example.distcomp.dto.response.NoteResponseTo +import com.example.distcomp.service.NoteService +import jakarta.validation.Valid +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/api/v1.0/notes") +class NoteController(private val service: NoteService) { + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + fun create(@Valid @RequestBody request: NoteRequestTo): NoteResponseTo { + return service.create(request) + } + + @GetMapping("/{id}") + fun getById(@PathVariable id: Long): NoteResponseTo { + return service.getById(id) + } + + @GetMapping + fun getAll(): List { + return service.getAll() + } + + @PutMapping("/{id}") + fun update(@PathVariable id: Long, @Valid @RequestBody request: NoteRequestTo): NoteResponseTo { + return service.patch(id, request) + } + + @PatchMapping("/{id}") + fun patch(@PathVariable id: Long, @Valid @RequestBody request: NoteRequestTo): NoteResponseTo { + return service.patch(id, request) + } + + @DeleteMapping("/{id}") + @ResponseStatus(HttpStatus.NO_CONTENT) + fun delete(@PathVariable id: Long) { + service.delete(id) + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/StickerController.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/StickerController.kt new file mode 100644 index 000000000..7f25f2378 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/StickerController.kt @@ -0,0 +1,45 @@ +package com.example.distcomp.controller + +import com.example.distcomp.dto.request.StickerRequestTo +import com.example.distcomp.dto.response.StickerResponseTo +import com.example.distcomp.service.StickerService +import jakarta.validation.Valid +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/api/v1.0/stickers") +class StickerController(private val service: StickerService) { + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + fun create(@Valid @RequestBody request: StickerRequestTo): StickerResponseTo { + return service.create(request) + } + + @GetMapping("/{id}") + fun getById(@PathVariable id: Long): StickerResponseTo { + return service.getById(id) + } + + @GetMapping + fun getAll(): List { + return service.getAll() + } + + @PutMapping("/{id}") + fun update(@PathVariable id: Long, @Valid @RequestBody request: StickerRequestTo): StickerResponseTo { + return service.patch(id, request) + } + + @PatchMapping("/{id}") + fun patch(@PathVariable id: Long, @Valid @RequestBody request: StickerRequestTo): StickerResponseTo { + return service.patch(id, request) + } + + @DeleteMapping("/{id}") + @ResponseStatus(HttpStatus.NO_CONTENT) + fun delete(@PathVariable id: Long) { + service.delete(id) + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/TweetController.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/TweetController.kt new file mode 100644 index 000000000..aeb6982ec --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/controller/TweetController.kt @@ -0,0 +1,63 @@ +package com.example.distcomp.controller + +import com.example.distcomp.dto.response.CreatorResponseTo +import com.example.distcomp.dto.response.NoteResponseTo +import com.example.distcomp.dto.response.StickerResponseTo +import com.example.distcomp.dto.request.TweetRequestTo +import com.example.distcomp.dto.response.TweetResponseTo +import com.example.distcomp.service.TweetService +import jakarta.validation.Valid +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/api/v1.0/tweets") +class TweetController(private val service: TweetService) { + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + fun create(@Valid @RequestBody request: TweetRequestTo): TweetResponseTo { + return service.create(request) + } + + @GetMapping("/{id}") + fun getById(@PathVariable id: Long): TweetResponseTo { + return service.getById(id) + } + + @GetMapping + fun getAll(): List { + return service.getAll() + } + + @PutMapping("/{id}") + fun update(@PathVariable id: Long, @Valid @RequestBody request: TweetRequestTo): TweetResponseTo { + return service.patch(id, request) + } + + @PatchMapping("/{id}") + fun patch(@PathVariable id: Long, @Valid @RequestBody request: TweetRequestTo): TweetResponseTo { + return service.patch(id, request) + } + + @GetMapping("/{id}/creator") + fun getCreatorByTweetId(@PathVariable id: Long): CreatorResponseTo { + return service.getCreatorByTweetId(id) + } + + @GetMapping("/{id}/stickers") + fun getStickersByTweetId(@PathVariable id: Long): List { + return service.getStickersByTweetId(id) + } + + @GetMapping("/{id}/notes") + fun getNotesByTweetId(@PathVariable id: Long): List { + return service.getNotesByTweetId(id) + } + + @DeleteMapping("/{id}") + @ResponseStatus(HttpStatus.NO_CONTENT) + fun delete(@PathVariable id: Long) { + service.delete(id) + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/CreatorRequestTo.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/CreatorRequestTo.kt new file mode 100644 index 000000000..fcc9f8cb1 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/CreatorRequestTo.kt @@ -0,0 +1,16 @@ +package com.example.distcomp.dto.request + +import com.fasterxml.jackson.annotation.JsonRootName +import jakarta.validation.constraints.Size + +@JsonRootName("creator") +data class CreatorRequestTo( + @field:Size(min = 2, max = 64) + var login: String? = null, + @field:Size(min = 8, max = 128) + var password: String? = null, + @field:Size(min = 2, max = 64) + var firstname: String? = null, + @field:Size(min = 2, max = 64) + var lastname: String? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/NoteRequestTo.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/NoteRequestTo.kt new file mode 100644 index 000000000..0ba8e6821 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/NoteRequestTo.kt @@ -0,0 +1,11 @@ +package com.example.distcomp.dto.request + +import com.fasterxml.jackson.annotation.JsonRootName +import jakarta.validation.constraints.Size + +@JsonRootName("note") +data class NoteRequestTo( + var tweetId: Long? = null, + @field:Size(min = 2, max = 2048) + var content: String? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/StickerRequestTo.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/StickerRequestTo.kt new file mode 100644 index 000000000..2215fb08c --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/StickerRequestTo.kt @@ -0,0 +1,10 @@ +package com.example.distcomp.dto.request + +import com.fasterxml.jackson.annotation.JsonRootName +import jakarta.validation.constraints.Size + +@JsonRootName("sticker") +data class StickerRequestTo( + @field:Size(min = 2, max = 32) + var name: String? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/TweetRequestTo.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/TweetRequestTo.kt new file mode 100644 index 000000000..b6b9f37c8 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/request/TweetRequestTo.kt @@ -0,0 +1,13 @@ +package com.example.distcomp.dto.request + +import com.fasterxml.jackson.annotation.JsonRootName +import jakarta.validation.constraints.Size + +@JsonRootName("tweet") +data class TweetRequestTo( + var creatorId: Long? = null, + @field:Size(min = 2, max = 64) + var title: String? = null, + @field:Size(min = 2, max = 2048) + var content: String? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/CreatorResponseTo.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/CreatorResponseTo.kt new file mode 100644 index 000000000..9099c79d9 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/CreatorResponseTo.kt @@ -0,0 +1,11 @@ +package com.example.distcomp.dto.response + +import com.fasterxml.jackson.annotation.JsonRootName + +@JsonRootName("creator") +data class CreatorResponseTo( + var id: Long? = null, + var login: String? = null, + var firstname: String? = null, + var lastname: String? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/NoteResponseTo.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/NoteResponseTo.kt new file mode 100644 index 000000000..f18dc0e46 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/NoteResponseTo.kt @@ -0,0 +1,10 @@ +package com.example.distcomp.dto.response + +import com.fasterxml.jackson.annotation.JsonRootName + +@JsonRootName("note") +data class NoteResponseTo( + var id: Long? = null, + var tweetId: Long? = null, + var content: String? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/StickerResponseTo.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/StickerResponseTo.kt new file mode 100644 index 000000000..5ce1f0e43 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/StickerResponseTo.kt @@ -0,0 +1,9 @@ +package com.example.distcomp.dto.response + +import com.fasterxml.jackson.annotation.JsonRootName + +@JsonRootName("sticker") +data class StickerResponseTo( + var id: Long? = null, + var name: String? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/TweetResponseTo.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/TweetResponseTo.kt new file mode 100644 index 000000000..108eee9fe --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/dto/response/TweetResponseTo.kt @@ -0,0 +1,14 @@ +package com.example.distcomp.dto.response + +import com.fasterxml.jackson.annotation.JsonRootName +import java.time.LocalDateTime + +@JsonRootName("tweet") +data class TweetResponseTo( + var id: Long? = null, + var creatorId: Long? = null, + var title: String? = null, + var content: String? = null, + var created: LocalDateTime? = null, + var modified: LocalDateTime? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/BadRequestException.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/BadRequestException.kt new file mode 100644 index 000000000..6c809f802 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/BadRequestException.kt @@ -0,0 +1,7 @@ +package com.example.distcomp.exception + +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.ResponseStatus + +@ResponseStatus(HttpStatus.BAD_REQUEST) +class BadRequestException(message: String) : RuntimeException(message) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/ConflictException.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/ConflictException.kt new file mode 100644 index 000000000..98148c50a --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/ConflictException.kt @@ -0,0 +1,7 @@ +package com.example.distcomp.exception + +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.ResponseStatus + +@ResponseStatus(HttpStatus.CONFLICT) +class ConflictException(message: String) : RuntimeException(message) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/ErrorResponse.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/ErrorResponse.kt new file mode 100644 index 000000000..da8215d65 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/ErrorResponse.kt @@ -0,0 +1,6 @@ +package com.example.distcomp.exception + +data class ErrorResponse( + val message: String, + val code: Int +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/GlobalExceptionHandler.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/GlobalExceptionHandler.kt new file mode 100644 index 000000000..479699ff8 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/GlobalExceptionHandler.kt @@ -0,0 +1,37 @@ +package com.example.distcomp.exception + +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 + +@ControllerAdvice +class GlobalExceptionHandler { + + @ExceptionHandler(NotFoundException::class) + fun handleNotFound(ex: NotFoundException): ResponseEntity { + return ResponseEntity(ErrorResponse(ex.message ?: "Not Found", 404), HttpStatus.NOT_FOUND) + } + + @ExceptionHandler(ConflictException::class) + fun handleConflict(ex: ConflictException): ResponseEntity { + return ResponseEntity(ErrorResponse(ex.message ?: "Conflict", 409), HttpStatus.CONFLICT) + } + + @ExceptionHandler(BadRequestException::class) + fun handleBadRequest(ex: BadRequestException): ResponseEntity { + return ResponseEntity(ErrorResponse(ex.message ?: "Bad Request", 400), HttpStatus.BAD_REQUEST) + } + + @ExceptionHandler(MethodArgumentNotValidException::class) + fun handleValidation(ex: MethodArgumentNotValidException): ResponseEntity { + val message = ex.bindingResult.allErrors.joinToString("; ") { it.defaultMessage ?: "Error" } + return ResponseEntity(ErrorResponse(message, 400), HttpStatus.BAD_REQUEST) + } + + @ExceptionHandler(Exception::class) + fun handleGeneral(ex: Exception): ResponseEntity { + return ResponseEntity(ErrorResponse(ex.message ?: "Internal Error", 500), HttpStatus.INTERNAL_SERVER_ERROR) + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/NotFoundException.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/NotFoundException.kt new file mode 100644 index 000000000..7f06c1613 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/exception/NotFoundException.kt @@ -0,0 +1,7 @@ +package com.example.distcomp.exception + +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.ResponseStatus + +@ResponseStatus(HttpStatus.NOT_FOUND) +class NotFoundException(message: String) : RuntimeException(message) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/CreatorMapper.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/CreatorMapper.kt new file mode 100644 index 000000000..c86a48405 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/CreatorMapper.kt @@ -0,0 +1,12 @@ +package com.example.distcomp.mapper + +import com.example.distcomp.dto.request.CreatorRequestTo +import com.example.distcomp.dto.response.CreatorResponseTo +import com.example.distcomp.model.Creator +import org.mapstruct.Mapper + +@Mapper(componentModel = "spring") +interface CreatorMapper { + fun toEntity(request: CreatorRequestTo): Creator + fun toResponse(entity: Creator): CreatorResponseTo +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/NoteMapper.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/NoteMapper.kt new file mode 100644 index 000000000..07947e7c4 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/NoteMapper.kt @@ -0,0 +1,12 @@ +package com.example.distcomp.mapper + +import com.example.distcomp.dto.request.NoteRequestTo +import com.example.distcomp.dto.response.NoteResponseTo +import com.example.distcomp.model.Note +import org.mapstruct.Mapper + +@Mapper(componentModel = "spring") +interface NoteMapper { + fun toEntity(request: NoteRequestTo): Note + fun toResponse(entity: Note): NoteResponseTo +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/StickerMapper.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/StickerMapper.kt new file mode 100644 index 000000000..f499d126b --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/StickerMapper.kt @@ -0,0 +1,12 @@ +package com.example.distcomp.mapper + +import com.example.distcomp.dto.request.StickerRequestTo +import com.example.distcomp.dto.response.StickerResponseTo +import com.example.distcomp.model.Sticker +import org.mapstruct.Mapper + +@Mapper(componentModel = "spring") +interface StickerMapper { + fun toEntity(request: StickerRequestTo): Sticker + fun toResponse(entity: Sticker): StickerResponseTo +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/TweetMapper.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/TweetMapper.kt new file mode 100644 index 000000000..847f378d5 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/mapper/TweetMapper.kt @@ -0,0 +1,12 @@ +package com.example.distcomp.mapper + +import com.example.distcomp.dto.request.TweetRequestTo +import com.example.distcomp.dto.response.TweetResponseTo +import com.example.distcomp.model.Tweet +import org.mapstruct.Mapper + +@Mapper(componentModel = "spring") +interface TweetMapper { + fun toEntity(request: TweetRequestTo): Tweet + fun toResponse(entity: Tweet): TweetResponseTo +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/BaseEntity.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/BaseEntity.kt new file mode 100644 index 000000000..e9a2a9966 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/BaseEntity.kt @@ -0,0 +1,5 @@ +package com.example.distcomp.model + +abstract class BaseEntity( + var id: Long? = null +) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Creator.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Creator.kt new file mode 100644 index 000000000..fd5ae7ee7 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Creator.kt @@ -0,0 +1,9 @@ +package com.example.distcomp.model + +class Creator( + id: Long? = null, + var login: String = "", + var password: String = "", + var firstname: String = "", + var lastname: String = "" +) : BaseEntity(id) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Note.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Note.kt new file mode 100644 index 000000000..02e956e56 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Note.kt @@ -0,0 +1,7 @@ +package com.example.distcomp.model + +class Note( + id: Long? = null, + var tweetId: Long? = null, + var content: String = "" +) : BaseEntity(id) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Sticker.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Sticker.kt new file mode 100644 index 000000000..c08901069 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Sticker.kt @@ -0,0 +1,6 @@ +package com.example.distcomp.model + +class Sticker( + id: Long? = null, + var name: String = "" +) : BaseEntity(id) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Tweet.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Tweet.kt new file mode 100644 index 000000000..917691322 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/model/Tweet.kt @@ -0,0 +1,13 @@ +package com.example.distcomp.model + +import java.time.LocalDateTime + +class Tweet( + id: Long? = null, + var creatorId: Long? = null, + var title: String = "", + var content: String = "", + var stickerIds: Set = emptySet(), + var created: LocalDateTime? = null, + var modified: LocalDateTime? = null +) : BaseEntity(id) diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/CreatorRepository.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/CreatorRepository.kt new file mode 100644 index 000000000..eccb03961 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/CreatorRepository.kt @@ -0,0 +1,9 @@ +package com.example.distcomp.repository + +import com.example.distcomp.model.Creator +import org.springframework.stereotype.Repository + +@Repository +class CreatorRepository : InMemoryRepository() { + fun findByLogin(login: String) = storage.values.find { it.login == login } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/CrudRepository.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/CrudRepository.kt new file mode 100644 index 000000000..bc3e60075 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/CrudRepository.kt @@ -0,0 +1,11 @@ +package com.example.distcomp.repository + +import com.example.distcomp.model.BaseEntity + +interface CrudRepository { + fun save(entity: T): T + fun findById(id: ID): T? + fun findAll(): List + fun deleteById(id: ID): Boolean + fun existsById(id: ID): Boolean +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/InMemoryRepository.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/InMemoryRepository.kt new file mode 100644 index 000000000..dd452a75d --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/InMemoryRepository.kt @@ -0,0 +1,26 @@ +package com.example.distcomp.repository + +import com.example.distcomp.model.BaseEntity +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicLong + +abstract class InMemoryRepository : CrudRepository { + protected val storage = ConcurrentHashMap() + private val idCounter = AtomicLong(1) + + override fun save(entity: T): T { + if (entity.id == null) { + entity.id = idCounter.getAndIncrement() + } + storage[entity.id!!] = entity + return entity + } + + override fun findById(id: Long): T? = storage[id] + + override fun findAll(): List = storage.values.toList() + + override fun deleteById(id: Long): Boolean = storage.remove(id) != null + + override fun existsById(id: Long): Boolean = storage.containsKey(id) +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/NoteRepository.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/NoteRepository.kt new file mode 100644 index 000000000..1183108cc --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/NoteRepository.kt @@ -0,0 +1,9 @@ +package com.example.distcomp.repository + +import com.example.distcomp.model.Note +import org.springframework.stereotype.Repository + +@Repository +class NoteRepository : InMemoryRepository() { + fun findByTweetId(tweetId: Long): List = storage.values.filter { it.tweetId == tweetId } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/StickerRepository.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/StickerRepository.kt new file mode 100644 index 000000000..6f2eeb1fe --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/StickerRepository.kt @@ -0,0 +1,9 @@ +package com.example.distcomp.repository + +import com.example.distcomp.model.Sticker +import org.springframework.stereotype.Repository + +@Repository +class StickerRepository : InMemoryRepository() { + fun findByName(name: String) = storage.values.find { it.name == name } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/TweetRepository.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/TweetRepository.kt new file mode 100644 index 000000000..dde9445b6 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/repository/TweetRepository.kt @@ -0,0 +1,9 @@ +package com.example.distcomp.repository + +import com.example.distcomp.model.Tweet +import org.springframework.stereotype.Repository + +@Repository +class TweetRepository : InMemoryRepository() { + fun findByTitle(title: String) = storage.values.find { it.title == title } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/CreatorService.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/CreatorService.kt new file mode 100644 index 000000000..e70faad77 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/CreatorService.kt @@ -0,0 +1,57 @@ +package com.example.distcomp.service + +import com.example.distcomp.dto.request.CreatorRequestTo +import com.example.distcomp.dto.response.CreatorResponseTo +import com.example.distcomp.exception.ConflictException +import com.example.distcomp.exception.NotFoundException +import com.example.distcomp.mapper.CreatorMapper +import com.example.distcomp.repository.CreatorRepository +import org.springframework.stereotype.Service + +@Service +class CreatorService( + private val repository: CreatorRepository, + private val mapper: CreatorMapper +) { + fun create(request: CreatorRequestTo): CreatorResponseTo { + if (repository.findByLogin(request.login!!) != null) { + throw ConflictException("Creator with login ${request.login} already exists") + } + val entity = mapper.toEntity(request) + val saved = repository.save(entity) + return mapper.toResponse(saved) + } + + fun getById(id: Long): CreatorResponseTo { + val entity = repository.findById(id) ?: throw NotFoundException("Creator with id $id not found") + return mapper.toResponse(entity) + } + + fun getAll(): List { + return repository.findAll().map { mapper.toResponse(it) } + } + + fun patch(id: Long, request: CreatorRequestTo): CreatorResponseTo { + val existing = repository.findById(id) ?: throw NotFoundException("Creator with id $id not found") + + request.login?.let { + val other = repository.findByLogin(it) + if (other != null && other.id != id) { + throw ConflictException("Creator with login $it already exists") + } + existing.login = it + } + request.password?.let { existing.password = it } + request.firstname?.let { existing.firstname = it } + request.lastname?.let { existing.lastname = it } + + val saved = repository.save(existing) + return mapper.toResponse(saved) + } + + fun delete(id: Long) { + if (!repository.deleteById(id)) { + throw NotFoundException("Creator with id $id not found") + } + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/NoteService.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/NoteService.kt new file mode 100644 index 000000000..4275aff0c --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/NoteService.kt @@ -0,0 +1,59 @@ +package com.example.distcomp.service + +import com.example.distcomp.dto.request.NoteRequestTo +import com.example.distcomp.dto.response.NoteResponseTo +import com.example.distcomp.exception.NotFoundException +import com.example.distcomp.mapper.NoteMapper +import com.example.distcomp.repository.NoteRepository +import com.example.distcomp.repository.TweetRepository +import org.springframework.stereotype.Service + +@Service +class NoteService( + private val repository: NoteRepository, + private val tweetRepository: TweetRepository, + private val mapper: NoteMapper +) { + fun create(request: NoteRequestTo): NoteResponseTo { + if (request.tweetId != null && !tweetRepository.existsById(request.tweetId!!)) { + throw NotFoundException("Tweet with id ${request.tweetId} not found") + } + val entity = mapper.toEntity(request) + val saved = repository.save(entity) + return mapper.toResponse(saved) + } + + fun getById(id: Long): NoteResponseTo { + val entity = repository.findById(id) ?: throw NotFoundException("Note with id $id not found") + return mapper.toResponse(entity) + } + + fun getAll(): List { + return repository.findAll().map { mapper.toResponse(it) } + } + + fun patch(id: Long, request: NoteRequestTo): NoteResponseTo { + val existing = repository.findById(id) ?: throw NotFoundException("Note with id $id not found") + + request.tweetId?.let { + if (!tweetRepository.existsById(it)) { + throw NotFoundException("Tweet with id $it not found") + } + existing.tweetId = it + } + request.content?.let { existing.content = it } + + val saved = repository.save(existing) + return mapper.toResponse(saved) + } + + fun delete(id: Long) { + if (!repository.deleteById(id)) { + throw NotFoundException("Note with id $id not found") + } + } + + fun getNotesByTweetId(tweetId: Long): List { + return repository.findByTweetId(tweetId).map { mapper.toResponse(it) } + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/StickerService.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/StickerService.kt new file mode 100644 index 000000000..6129f7d6c --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/StickerService.kt @@ -0,0 +1,54 @@ +package com.example.distcomp.service + +import com.example.distcomp.dto.request.StickerRequestTo +import com.example.distcomp.dto.response.StickerResponseTo +import com.example.distcomp.exception.ConflictException +import com.example.distcomp.exception.NotFoundException +import com.example.distcomp.mapper.StickerMapper +import com.example.distcomp.repository.StickerRepository +import org.springframework.stereotype.Service + +@Service +class StickerService( + private val repository: StickerRepository, + private val mapper: StickerMapper +) { + fun create(request: StickerRequestTo): StickerResponseTo { + if (repository.findByName(request.name!!) != null) { + throw ConflictException("Sticker with name ${request.name} already exists") + } + val entity = mapper.toEntity(request) + val saved = repository.save(entity) + return mapper.toResponse(saved) + } + + fun getById(id: Long): StickerResponseTo { + val entity = repository.findById(id) ?: throw NotFoundException("Sticker with id $id not found") + return mapper.toResponse(entity) + } + + fun getAll(): List { + return repository.findAll().map { mapper.toResponse(it) } + } + + fun patch(id: Long, request: StickerRequestTo): StickerResponseTo { + val existing = repository.findById(id) ?: throw NotFoundException("Sticker with id $id not found") + + request.name?.let { + val other = repository.findByName(it) + if (other != null && other.id != id) { + throw ConflictException("Sticker with name $it already exists") + } + existing.name = it + } + + val saved = repository.save(existing) + return mapper.toResponse(saved) + } + + fun delete(id: Long) { + if (!repository.deleteById(id)) { + throw NotFoundException("Sticker with id $id not found") + } + } +} diff --git a/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/TweetService.kt b/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/TweetService.kt new file mode 100644 index 000000000..e2be00873 --- /dev/null +++ b/351002/Abrashin/src/main/kotlin/com/example/distcomp/service/TweetService.kt @@ -0,0 +1,91 @@ +package com.example.distcomp.service + +import com.example.distcomp.dto.request.TweetRequestTo +import com.example.distcomp.dto.response.TweetResponseTo +import com.example.distcomp.dto.response.CreatorResponseTo +import com.example.distcomp.dto.response.NoteResponseTo +import com.example.distcomp.dto.response.StickerResponseTo +import com.example.distcomp.exception.ConflictException +import com.example.distcomp.exception.NotFoundException +import com.example.distcomp.mapper.TweetMapper +import com.example.distcomp.repository.TweetRepository +import org.springframework.stereotype.Service +import java.time.LocalDateTime + +@Service +class TweetService( + private val repository: TweetRepository, + private val mapper: TweetMapper, + private val creatorService: CreatorService, + private val stickerService: StickerService, + private val noteService: NoteService +) { + fun create(request: TweetRequestTo): TweetResponseTo { + request.creatorId?.let { + creatorService.getById(it) + } + if (repository.findByTitle(request.title!!) != null) { + throw ConflictException("Tweet with title ${request.title} already exists") + } + val entity = mapper.toEntity(request) + val now = LocalDateTime.now() + entity.created = now + entity.modified = now + val saved = repository.save(entity) + return mapper.toResponse(saved) + } + + fun getById(id: Long): TweetResponseTo { + val entity = repository.findById(id) ?: throw NotFoundException("Tweet with id $id not found") + return mapper.toResponse(entity) + } + + fun getAll(): List { + return repository.findAll().map { mapper.toResponse(it) } + } + + fun patch(id: Long, request: TweetRequestTo): TweetResponseTo { + val existing = repository.findById(id) ?: throw NotFoundException("Tweet with id $id not found") + + request.creatorId?.let { + creatorService.getById(it) + existing.creatorId = it + } + request.title?.let { + val other = repository.findByTitle(it) + if (other != null && other.id != id) { + throw ConflictException("Tweet with title $it already exists") + } + existing.title = it + } + request.content?.let { existing.content = it } + + existing.modified = LocalDateTime.now() + val saved = repository.save(existing) + return mapper.toResponse(saved) + } + + fun getCreatorByTweetId(id: Long): CreatorResponseTo { + val tweet = repository.findById(id) ?: throw NotFoundException("Tweet with id $id not found") + val creatorId = tweet.creatorId ?: throw NotFoundException("Creator for tweet $id not found") + return creatorService.getById(creatorId) + } + + fun getStickersByTweetId(id: Long): List { + val tweet = repository.findById(id) ?: throw NotFoundException("Tweet with id $id not found") + return tweet.stickerIds.map { stickerId -> + stickerService.getById(stickerId) + } + } + + fun getNotesByTweetId(id: Long): List { + val tweet = repository.findById(id) ?: throw NotFoundException("Tweet with id $id not found") + return noteService.getNotesByTweetId(tweet.id!!) + } + + fun delete(id: Long) { + if (!repository.deleteById(id)) { + throw NotFoundException("Tweet with id $id not found") + } + } +} diff --git a/351002/Abrashin/src/main/resources/application.properties b/351002/Abrashin/src/main/resources/application.properties new file mode 100644 index 000000000..0bf6ca6e1 --- /dev/null +++ b/351002/Abrashin/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=24110 diff --git a/351002/Abrashin/src/test/kotlin/com/example/distcomp/DistCompApplicationTests.kt b/351002/Abrashin/src/test/kotlin/com/example/distcomp/DistCompApplicationTests.kt new file mode 100644 index 000000000..8459eeabf --- /dev/null +++ b/351002/Abrashin/src/test/kotlin/com/example/distcomp/DistCompApplicationTests.kt @@ -0,0 +1,13 @@ +package com.example.distcomp + +import org.junit.jupiter.api.Test +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +class DistCompApplicationTests { + + @Test + fun contextLoads() { + } + +}