diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..e0a836ab --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.gradle \ No newline at end of file diff --git a/channel.dat b/channel.dat new file mode 100644 index 00000000..6b578085 Binary files /dev/null and b/channel.dat differ diff --git a/discodeit.iml b/discodeit.iml new file mode 100644 index 00000000..d1fe2e04 --- /dev/null +++ b/discodeit.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/discodeit/.gitattributes b/discodeit/.gitattributes new file mode 100644 index 00000000..8af972cd --- /dev/null +++ b/discodeit/.gitattributes @@ -0,0 +1,3 @@ +/gradlew text eol=lf +*.bat text eol=crlf +*.jar binary diff --git a/discodeit/.gitignore b/discodeit/.gitignore new file mode 100644 index 00000000..c2065bc2 --- /dev/null +++ b/discodeit/.gitignore @@ -0,0 +1,37 @@ +HELP.md +.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/ diff --git a/discodeit/README.md b/discodeit/README.md new file mode 100644 index 00000000..815bede5 --- /dev/null +++ b/discodeit/README.md @@ -0,0 +1,2 @@ +# 0-spring-mission +스프린트 미션 모범 답안 리포지토리입니다. diff --git a/discodeit/build.gradle b/discodeit/build.gradle new file mode 100644 index 00000000..1d3800a6 --- /dev/null +++ b/discodeit/build.gradle @@ -0,0 +1,37 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.5.10' + id 'io.spring.dependency-management' version '1.1.7' +} + +group = 'com.sprint.mission' +version = '0.0.1-SNAPSHOT' +description = 'Demo project for Spring Boot' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/discodeit/gradle/wrapper/gradle-wrapper.jar b/discodeit/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..1b33c55b Binary files /dev/null and b/discodeit/gradle/wrapper/gradle-wrapper.jar differ diff --git a/discodeit/gradle/wrapper/gradle-wrapper.properties b/discodeit/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..aaaabb3c --- /dev/null +++ b/discodeit/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.4-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/discodeit/gradlew b/discodeit/gradlew new file mode 100644 index 00000000..23d15a93 --- /dev/null +++ b/discodeit/gradlew @@ -0,0 +1,251 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 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 + +CLASSPATH="\\\"\\\"" + + +# 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" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + 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" \ + -classpath "$CLASSPATH" \ + -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/discodeit/gradlew.bat b/discodeit/gradlew.bat new file mode 100644 index 00000000..db3a6ac2 --- /dev/null +++ b/discodeit/gradlew.bat @@ -0,0 +1,94 @@ +@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 + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -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/discodeit/settings.gradle b/discodeit/settings.gradle new file mode 100644 index 00000000..2437dfb2 --- /dev/null +++ b/discodeit/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'discodeit' diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/DiscodeitApplication.java b/discodeit/src/main/java/com/sprint/mission/discodeit/DiscodeitApplication.java new file mode 100644 index 00000000..6ebc70d8 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/DiscodeitApplication.java @@ -0,0 +1,47 @@ +package com.sprint.mission.discodeit; + +import com.sprint.mission.discodeit.entity.Channel; +import com.sprint.mission.discodeit.entity.ChannelType; +import com.sprint.mission.discodeit.entity.Message; +import com.sprint.mission.discodeit.entity.User; +import com.sprint.mission.discodeit.service.ChannelService; +import com.sprint.mission.discodeit.service.MessageService; +import com.sprint.mission.discodeit.service.UserService; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; + +@SpringBootApplication +public class DiscodeitApplication { + + static User setupUser(UserService userService) { + User user = userService.create("woody", "woody@codeit.com", "woody1234"); + return user; + } + + static Channel setupChannel(ChannelService channelService) { + Channel channel = channelService.create(ChannelType.PUBLIC, "공지", "공지 채널입니다."); + return channel; + } + + static void messageCreateTest(MessageService messageService, Channel channel, User author) { + Message message = messageService.create("안녕하세요.", channel.getId(), author.getId()); + System.out.println("메시지 생성: " + message.getId()); + } + + public static void main(String[] args) { + + ConfigurableApplicationContext context = SpringApplication.run(DiscodeitApplication.class, args); + // 서비스 초기화 + UserService userService = context.getBean(UserService.class); + ChannelService channelService = context.getBean(ChannelService.class); + MessageService messageService = context.getBean(MessageService.class); + + // 셋업 + User user = setupUser(userService); + Channel channel = setupChannel(channelService); + // 테스트 + messageCreateTest(messageService, channel, user); + } + +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/entity/BinaryContent.java b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/BinaryContent.java new file mode 100644 index 00000000..fdab7cdb --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/BinaryContent.java @@ -0,0 +1,22 @@ +package com.sprint.mission.discodeit.entity; + +import lombok.Getter; + +import java.io.Serializable; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +@Getter +public class BinaryContent implements Serializable { + private static final long serialVersionUID = 1L; + private UUID id; + private final Instant createdAt; + private String url; + + public BinaryContent(String url) { + this.id = UUID.randomUUID(); + this.createdAt = Instant.now(); + this.url = url; + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/entity/Channel.java b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/Channel.java new file mode 100644 index 00000000..786b8ef2 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/Channel.java @@ -0,0 +1,44 @@ +package com.sprint.mission.discodeit.entity; + +import lombok.Getter; + +import java.io.Serializable; +import java.time.Instant; +import java.util.UUID; + +@Getter +public class Channel implements Serializable { + private static final long serialVersionUID = 1L; + private UUID id; + private Instant createdAt; + private Instant updatedAt; + // + private ChannelType type; + private String name; + private String description; + + public Channel(ChannelType type, String name, String description) { + this.id = UUID.randomUUID(); + this.createdAt = Instant.now(); + // + this.type = type; + this.name = name; + this.description = description; + } + + public void update(String newName, String newDescription) { + boolean anyValueUpdated = false; + if (newName != null && !newName.equals(this.name)) { + this.name = newName; + anyValueUpdated = true; + } + if (newDescription != null && !newDescription.equals(this.description)) { + this.description = newDescription; + anyValueUpdated = true; + } + + if (anyValueUpdated) { + this.updatedAt = Instant.now(); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/entity/ChannelType.java b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/ChannelType.java new file mode 100644 index 00000000..9a2ff3f0 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/ChannelType.java @@ -0,0 +1,6 @@ +package com.sprint.mission.discodeit.entity; + +public enum ChannelType { + PUBLIC, + PRIVATE, +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/entity/Message.java b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/Message.java new file mode 100644 index 00000000..ce1216f3 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/Message.java @@ -0,0 +1,41 @@ +package com.sprint.mission.discodeit.entity; + +import lombok.Getter; + +import java.io.Serializable; +import java.time.Instant; +import java.util.UUID; + +@Getter +public class Message implements Serializable { + private static final long serialVersionUID = 1L; + + private UUID id; + private UUID attachmentId; + private Instant createdAt; + private Instant updatedAt; + private String content; + private UUID channelId; + private UUID authorId; + + public Message(String content, UUID channelId, UUID authorId) { + this.id = UUID.randomUUID(); + this.createdAt = Instant.now(); + // + this.content = content; + this.channelId = channelId; + this.authorId = authorId; + } + + public void update(String newContent) { + boolean anyValueUpdated = false; + if (newContent != null && !newContent.equals(this.content)) { + this.content = newContent; + anyValueUpdated = true; + } + + if (anyValueUpdated) { + this.updatedAt = Instant.now(); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/entity/ReadStatus.java b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/ReadStatus.java new file mode 100644 index 00000000..6a9277ad --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/ReadStatus.java @@ -0,0 +1,29 @@ +package com.sprint.mission.discodeit.entity; + +import lombok.Getter; + +import java.io.Serializable; +import java.time.Instant; +import java.util.UUID; + +@Getter +public class ReadStatus implements Serializable { + private static final long serialVersionUID = 1L; + private UUID id; + private UUID userId; + private UUID channelId; + private final Instant createdAt; + private Instant lastReadAt; + + public ReadStatus(UUID userId, UUID channelId) { + this.id = UUID.randomUUID(); + this.userId = userId; + this.channelId = channelId; + this.createdAt = Instant.now(); + this.lastReadAt = createdAt; + } + + public void update() { + this.lastReadAt = Instant.now(); + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/entity/User.java b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/User.java new file mode 100644 index 00000000..8aa45cd8 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/User.java @@ -0,0 +1,50 @@ +package com.sprint.mission.discodeit.entity; + +import lombok.Getter; + +import java.io.Serializable; +import java.time.Instant; +import java.util.UUID; + +@Getter +public class User implements Serializable { + private static final long serialVersionUID = 1L; + + private UUID id; + private UUID profileId; + private Instant createdAt; + private Instant updatedAt; + // + private String username; + private String email; + private String password; + + public User(String username, String email, String password) { + this.id = UUID.randomUUID(); + this.createdAt = Instant.now(); + // + this.username = username; + this.email = email; + this.password = password; + } + + public void update(String newUsername, String newEmail, String newPassword) { + boolean anyValueUpdated = false; + if (newUsername != null && !newUsername.equals(this.username)) { + this.username = newUsername; + anyValueUpdated = true; + } + if (newEmail != null && !newEmail.equals(this.email)) { + this.email = newEmail; + anyValueUpdated = true; + } + if (newPassword != null && !newPassword.equals(this.password)) { + this.password = newPassword; + anyValueUpdated = true; + } + + if (anyValueUpdated) { + this.updatedAt = Instant.now(); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/entity/UserStatus.java b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/UserStatus.java new file mode 100644 index 00000000..7d22b827 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/entity/UserStatus.java @@ -0,0 +1,33 @@ +package com.sprint.mission.discodeit.entity; + +import lombok.Getter; + +import java.io.Serializable; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +@Getter +public class UserStatus implements Serializable { + private static final long serialVersionUID = 1L; + private UUID id; + private UUID userId; + private final Instant createdAt; + private Instant lastJoinAt; + + public UserStatus(UUID userId) { + this.id = UUID.randomUUID(); + this.userId = userId; + this.createdAt = Instant.now(); + this.lastJoinAt = createdAt; + } + + public void update() { + this.lastJoinAt = Instant.now(); + } + + public boolean isOnline() { + Instant now = Instant.now(); + return this.lastJoinAt.isAfter(Instant.now().minus(5, ChronoUnit.MINUTES)); + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/record/UserCreateRequest.java b/discodeit/src/main/java/com/sprint/mission/discodeit/record/UserCreateRequest.java new file mode 100644 index 00000000..605f5fe4 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/record/UserCreateRequest.java @@ -0,0 +1,8 @@ +package com.sprint.mission.discodeit.record; + +public record UserCreateRequest( + String userName, + String email, + String password, + String profileUrl +) {} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/BinaryContentRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/BinaryContentRepository.java new file mode 100644 index 00000000..4891a35f --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/BinaryContentRepository.java @@ -0,0 +1,15 @@ +package com.sprint.mission.discodeit.repository; + +import com.sprint.mission.discodeit.entity.BinaryContent; +import com.sprint.mission.discodeit.entity.Channel; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface BinaryContentRepository { + BinaryContent save(BinaryContent binaryContent); + Optional findById(UUID id); + List findAll(); + boolean existsById(UUID id); + void deleteById(UUID id); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/ChannelRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/ChannelRepository.java new file mode 100644 index 00000000..326a84f4 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/ChannelRepository.java @@ -0,0 +1,15 @@ +package com.sprint.mission.discodeit.repository; + +import com.sprint.mission.discodeit.entity.Channel; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface ChannelRepository { + Channel save(Channel channel); + Optional findById(UUID id); + List findAll(); + boolean existsById(UUID id); + void deleteById(UUID id); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/MessageRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/MessageRepository.java new file mode 100644 index 00000000..1477de72 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/MessageRepository.java @@ -0,0 +1,15 @@ +package com.sprint.mission.discodeit.repository; + +import com.sprint.mission.discodeit.entity.Message; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface MessageRepository { + Message save(Message message); + Optional findById(UUID id); + List findAll(); + boolean existsById(UUID id); + void deleteById(UUID id); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/ReadStatusRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/ReadStatusRepository.java new file mode 100644 index 00000000..eeb401d0 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/ReadStatusRepository.java @@ -0,0 +1,16 @@ +package com.sprint.mission.discodeit.repository; + +import com.sprint.mission.discodeit.entity.Channel; +import com.sprint.mission.discodeit.entity.ReadStatus; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface ReadStatusRepository { + ReadStatus save(ReadStatus readStatus); + Optional findById(UUID id); + List findAll(); + boolean existsById(UUID id); + void deleteById(UUID id); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/UserRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/UserRepository.java new file mode 100644 index 00000000..46464718 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/UserRepository.java @@ -0,0 +1,17 @@ +package com.sprint.mission.discodeit.repository; + +import com.sprint.mission.discodeit.entity.User; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface UserRepository { + User save(User user); + Optional findById(UUID id); + List findAll(); + boolean existsById(UUID id); + boolean existsByUsername(String username); + boolean existsByEmail(String email); + void deleteById(UUID id); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/UserStatusRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/UserStatusRepository.java new file mode 100644 index 00000000..0f4e7dbd --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/UserStatusRepository.java @@ -0,0 +1,16 @@ +package com.sprint.mission.discodeit.repository; + +import com.sprint.mission.discodeit.entity.Channel; +import com.sprint.mission.discodeit.entity.UserStatus; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface UserStatusRepository { + UserStatus save(UserStatus userStatus); + Optional findById(UUID id); + List findAll(); + boolean existsById(UUID id); + void deleteById(UUID id); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileBinaryContentRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileBinaryContentRepository.java new file mode 100644 index 00000000..39d97416 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileBinaryContentRepository.java @@ -0,0 +1,102 @@ +package com.sprint.mission.discodeit.repository.file; + +import com.sprint.mission.discodeit.entity.BinaryContent; +import com.sprint.mission.discodeit.repository.BinaryContentRepository; +import org.springframework.stereotype.Repository; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Repository +public class FileBinaryContentRepository implements BinaryContentRepository { + private final Path DIRECTORY; + private final String EXTENSION = ".ser"; + + public FileBinaryContentRepository() { + this.DIRECTORY = Paths.get(System.getProperty("user.dir"), "file-data-map", BinaryContent.class.getSimpleName()); + if (Files.notExists(DIRECTORY)) { + try { + Files.createDirectories(DIRECTORY); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private Path resolvePath(UUID id) { + return DIRECTORY.resolve(id + EXTENSION); + } + + @Override + public BinaryContent save(BinaryContent binaryContent) { + Path path = resolvePath(binaryContent.getId()); + try ( + FileOutputStream fos = new FileOutputStream(path.toFile()); + ObjectOutputStream oos = new ObjectOutputStream(fos) + ) { + oos.writeObject(binaryContent); + } catch (IOException e) { + throw new RuntimeException(e); + } + return binaryContent; + } + + @Override + public Optional findById(UUID id) { + BinaryContent binaryContentNullable = null; + Path path = resolvePath(id); + if (Files.exists(path)) { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + binaryContentNullable = (BinaryContent) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + return Optional.ofNullable(binaryContentNullable); + } + + @Override + public List findAll() { + try { + return Files.list(DIRECTORY) + .filter(path -> path.toString().endsWith(EXTENSION)) + .map(path -> { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + return (BinaryContent) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + }) + .toList(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean existsById(UUID id) { + Path path = resolvePath(id); + return Files.exists(path); + } + + @Override + public void deleteById(UUID id) { + Path path = resolvePath(id); + try { + Files.delete(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileChannelRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileChannelRepository.java new file mode 100644 index 00000000..48b7de34 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileChannelRepository.java @@ -0,0 +1,102 @@ +package com.sprint.mission.discodeit.repository.file; + +import com.sprint.mission.discodeit.entity.Channel; +import com.sprint.mission.discodeit.repository.ChannelRepository; +import org.springframework.stereotype.Repository; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Repository +public class FileChannelRepository implements ChannelRepository { + private final Path DIRECTORY; + private final String EXTENSION = ".ser"; + + public FileChannelRepository() { + this.DIRECTORY = Paths.get(System.getProperty("user.dir"), "file-data-map", Channel.class.getSimpleName()); + if (Files.notExists(DIRECTORY)) { + try { + Files.createDirectories(DIRECTORY); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private Path resolvePath(UUID id) { + return DIRECTORY.resolve(id + EXTENSION); + } + + @Override + public Channel save(Channel channel) { + Path path = resolvePath(channel.getId()); + try ( + FileOutputStream fos = new FileOutputStream(path.toFile()); + ObjectOutputStream oos = new ObjectOutputStream(fos) + ) { + oos.writeObject(channel); + } catch (IOException e) { + throw new RuntimeException(e); + } + return channel; + } + + @Override + public Optional findById(UUID id) { + Channel channelNullable = null; + Path path = resolvePath(id); + if (Files.exists(path)) { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + channelNullable = (Channel) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + return Optional.ofNullable(channelNullable); + } + + @Override + public List findAll() { + try { + return Files.list(DIRECTORY) + .filter(path -> path.toString().endsWith(EXTENSION)) + .map(path -> { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + return (Channel) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + }) + .toList(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean existsById(UUID id) { + Path path = resolvePath(id); + return Files.exists(path); + } + + @Override + public void deleteById(UUID id) { + Path path = resolvePath(id); + try { + Files.delete(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileMessageRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileMessageRepository.java new file mode 100644 index 00000000..bf71910f --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileMessageRepository.java @@ -0,0 +1,102 @@ +package com.sprint.mission.discodeit.repository.file; + +import com.sprint.mission.discodeit.entity.Message; +import com.sprint.mission.discodeit.repository.MessageRepository; +import org.springframework.stereotype.Repository; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Repository +public class FileMessageRepository implements MessageRepository { + private final Path DIRECTORY; + private final String EXTENSION = ".ser"; + + public FileMessageRepository() { + this.DIRECTORY = Paths.get(System.getProperty("user.dir"), "file-data-map", Message.class.getSimpleName()); + if (Files.notExists(DIRECTORY)) { + try { + Files.createDirectories(DIRECTORY); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private Path resolvePath(UUID id) { + return DIRECTORY.resolve(id + EXTENSION); + } + + @Override + public Message save(Message message) { + Path path = resolvePath(message.getId()); + try ( + FileOutputStream fos = new FileOutputStream(path.toFile()); + ObjectOutputStream oos = new ObjectOutputStream(fos) + ) { + oos.writeObject(message); + } catch (IOException e) { + throw new RuntimeException(e); + } + return message; + } + + @Override + public Optional findById(UUID id) { + Message messageNullable = null; + Path path = resolvePath(id); + if (Files.exists(path)) { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + messageNullable = (Message) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + return Optional.ofNullable(messageNullable); + } + + @Override + public List findAll() { + try { + return Files.list(DIRECTORY) + .filter(path -> path.toString().endsWith(EXTENSION)) + .map(path -> { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + return (Message) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + }) + .toList(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean existsById(UUID id) { + Path path = resolvePath(id); + return Files.exists(path); + } + + @Override + public void deleteById(UUID id) { + Path path = resolvePath(id); + try { + Files.delete(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileReadStatusRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileReadStatusRepository.java new file mode 100644 index 00000000..90e81ab9 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileReadStatusRepository.java @@ -0,0 +1,104 @@ +package com.sprint.mission.discodeit.repository.file; + +import com.sprint.mission.discodeit.entity.ReadStatus; +import com.sprint.mission.discodeit.entity.UserStatus; +import com.sprint.mission.discodeit.repository.ReadStatusRepository; +import com.sprint.mission.discodeit.repository.UserStatusRepository; +import org.springframework.stereotype.Repository; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Repository +public class FileReadStatusRepository implements ReadStatusRepository { + private final Path DIRECTORY; + private final String EXTENSION = ".ser"; + + public FileReadStatusRepository() { + this.DIRECTORY = Paths.get(System.getProperty("user.dir"), "file-data-map", ReadStatus.class.getSimpleName()); + if (Files.notExists(DIRECTORY)) { + try { + Files.createDirectories(DIRECTORY); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private Path resolvePath(UUID id) { + return DIRECTORY.resolve(id + EXTENSION); + } + + @Override + public ReadStatus save(ReadStatus readStatus) { + Path path = resolvePath(readStatus.getId()); + try ( + FileOutputStream fos = new FileOutputStream(path.toFile()); + ObjectOutputStream oos = new ObjectOutputStream(fos) + ) { + oos.writeObject(readStatus); + } catch (IOException e) { + throw new RuntimeException(e); + } + return readStatus; + } + + @Override + public Optional findById(UUID id) { + ReadStatus readStatusNullable = null; + Path path = resolvePath(id); + if (Files.exists(path)) { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + readStatusNullable = (ReadStatus) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + return Optional.ofNullable(readStatusNullable); + } + + @Override + public List findAll() { + try { + return Files.list(DIRECTORY) + .filter(path -> path.toString().endsWith(EXTENSION)) + .map(path -> { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + return (ReadStatus) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + }) + .toList(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean existsById(UUID id) { + Path path = resolvePath(id); + return Files.exists(path); + } + + @Override + public void deleteById(UUID id) { + Path path = resolvePath(id); + try { + Files.delete(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileUserRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileUserRepository.java new file mode 100644 index 00000000..94427289 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileUserRepository.java @@ -0,0 +1,112 @@ +package com.sprint.mission.discodeit.repository.file; + +import com.sprint.mission.discodeit.entity.User; +import com.sprint.mission.discodeit.repository.UserRepository; +import org.springframework.stereotype.Repository; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Repository +public class FileUserRepository implements UserRepository { + private final Path DIRECTORY; + private final String EXTENSION = ".ser"; + + public FileUserRepository() { + this.DIRECTORY = Paths.get(System.getProperty("user.dir"), "file-data-map", User.class.getSimpleName()); + if (Files.notExists(DIRECTORY)) { + try { + Files.createDirectories(DIRECTORY); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private Path resolvePath(UUID id) { + return DIRECTORY.resolve(id + EXTENSION); + } + + @Override + public User save(User user) { + Path path = resolvePath(user.getId()); + try ( + FileOutputStream fos = new FileOutputStream(path.toFile()); + ObjectOutputStream oos = new ObjectOutputStream(fos) + ) { + oos.writeObject(user); + } catch (IOException e) { + throw new RuntimeException(e); + } + return user; + } + + @Override + public Optional findById(UUID id) { + User userNullable = null; + Path path = resolvePath(id); + if (Files.exists(path)) { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + userNullable = (User) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + return Optional.ofNullable(userNullable); + } + + @Override + public List findAll() { + try { + return Files.list(DIRECTORY) + .filter(path -> path.toString().endsWith(EXTENSION)) + .map(path -> { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + return (User) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + }) + .toList(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean existsById(UUID id) { + Path path = resolvePath(id); + return Files.exists(path); + } + + @Override + public boolean existsByUsername(String username) { + return findAll().stream().anyMatch(user -> user.getUsername().equals(username)); + } + + @Override + public boolean existsByEmail(String email) { + return findAll().stream().anyMatch(user -> user.getEmail().equals(email)); + } + + @Override + public void deleteById(UUID id) { + Path path = resolvePath(id); + try { + Files.delete(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileUserStatusRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileUserStatusRepository.java new file mode 100644 index 00000000..ca25fcf0 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/file/FileUserStatusRepository.java @@ -0,0 +1,104 @@ +package com.sprint.mission.discodeit.repository.file; + +import com.sprint.mission.discodeit.entity.BinaryContent; +import com.sprint.mission.discodeit.entity.User; +import com.sprint.mission.discodeit.entity.UserStatus; +import com.sprint.mission.discodeit.repository.UserStatusRepository; +import org.springframework.stereotype.Repository; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Repository +public class FileUserStatusRepository implements UserStatusRepository { + private final Path DIRECTORY; + private final String EXTENSION = ".ser"; + + public FileUserStatusRepository() { + this.DIRECTORY = Paths.get(System.getProperty("user.dir"), "file-data-map", UserStatus.class.getSimpleName()); + if (Files.notExists(DIRECTORY)) { + try { + Files.createDirectories(DIRECTORY); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private Path resolvePath(UUID id) { + return DIRECTORY.resolve(id + EXTENSION); + } + + @Override + public UserStatus save(UserStatus userStatus) { + Path path = resolvePath(userStatus.getId()); + try ( + FileOutputStream fos = new FileOutputStream(path.toFile()); + ObjectOutputStream oos = new ObjectOutputStream(fos) + ) { + oos.writeObject(userStatus); + } catch (IOException e) { + throw new RuntimeException(e); + } + return userStatus; + } + + @Override + public Optional findById(UUID id) { + UserStatus userStatusNullable = null; + Path path = resolvePath(id); + if (Files.exists(path)) { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + userStatusNullable = (UserStatus) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + return Optional.ofNullable(userStatusNullable); + } + + @Override + public List findAll() { + try { + return Files.list(DIRECTORY) + .filter(path -> path.toString().endsWith(EXTENSION)) + .map(path -> { + try ( + FileInputStream fis = new FileInputStream(path.toFile()); + ObjectInputStream ois = new ObjectInputStream(fis) + ) { + return (UserStatus) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + }) + .toList(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean existsById(UUID id) { + Path path = resolvePath(id); + return Files.exists(path); + } + + @Override + public void deleteById(UUID id) { + Path path = resolvePath(id); + try { + Files.delete(path); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFChannelRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFChannelRepository.java new file mode 100644 index 00000000..e11c92da --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFChannelRepository.java @@ -0,0 +1,40 @@ +package com.sprint.mission.discodeit.repository.jcf; + +import com.sprint.mission.discodeit.entity.Channel; +import com.sprint.mission.discodeit.repository.ChannelRepository; + +import java.util.*; + +public class JCFChannelRepository implements ChannelRepository { + private final Map data; + + public JCFChannelRepository() { + this.data = new HashMap<>(); + } + + @Override + public Channel save(Channel channel) { + this.data.put(channel.getId(), channel); + return channel; + } + + @Override + public Optional findById(UUID id) { + return Optional.ofNullable(this.data.get(id)); + } + + @Override + public List findAll() { + return this.data.values().stream().toList(); + } + + @Override + public boolean existsById(UUID id) { + return this.data.containsKey(id); + } + + @Override + public void deleteById(UUID id) { + this.data.remove(id); + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFMessageRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFMessageRepository.java new file mode 100644 index 00000000..5ad8b355 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFMessageRepository.java @@ -0,0 +1,40 @@ +package com.sprint.mission.discodeit.repository.jcf; + +import com.sprint.mission.discodeit.entity.Message; +import com.sprint.mission.discodeit.repository.MessageRepository; + +import java.util.*; + +public class JCFMessageRepository implements MessageRepository { + private final Map data; + + public JCFMessageRepository() { + this.data = new HashMap<>(); + } + + @Override + public Message save(Message message) { + this.data.put(message.getId(), message); + return message; + } + + @Override + public Optional findById(UUID id) { + return Optional.ofNullable(this.data.get(id)); + } + + @Override + public List findAll() { + return this.data.values().stream().toList(); + } + + @Override + public boolean existsById(UUID id) { + return this.data.containsKey(id); + } + + @Override + public void deleteById(UUID id) { + this.data.remove(id); + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFUserRepository.java b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFUserRepository.java new file mode 100644 index 00000000..a60363a3 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/repository/jcf/JCFUserRepository.java @@ -0,0 +1,40 @@ +package com.sprint.mission.discodeit.repository.jcf; + +import com.sprint.mission.discodeit.entity.User; +import com.sprint.mission.discodeit.repository.UserRepository; + +import java.util.*; + +public class JCFUserRepository implements UserRepository { + private final Map data; + + public JCFUserRepository() { + this.data = new HashMap<>(); + } + + @Override + public User save(User user) { + this.data.put(user.getId(), user); + return user; + } + + @Override + public Optional findById(UUID id) { + return Optional.ofNullable(this.data.get(id)); + } + + @Override + public List findAll() { + return this.data.values().stream().toList(); + } + + @Override + public boolean existsById(UUID id) { + return this.data.containsKey(id); + } + + @Override + public void deleteById(UUID id) { + this.data.remove(id); + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/service/ChannelService.java b/discodeit/src/main/java/com/sprint/mission/discodeit/service/ChannelService.java new file mode 100644 index 00000000..eaa8e07d --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/service/ChannelService.java @@ -0,0 +1,15 @@ +package com.sprint.mission.discodeit.service; + +import com.sprint.mission.discodeit.entity.Channel; +import com.sprint.mission.discodeit.entity.ChannelType; + +import java.util.List; +import java.util.UUID; + +public interface ChannelService { + Channel create(ChannelType type, String name, String description); + Channel find(UUID channelId); + List findAll(); + Channel update(UUID channelId, String newName, String newDescription); + void delete(UUID channelId); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/service/MessageService.java b/discodeit/src/main/java/com/sprint/mission/discodeit/service/MessageService.java new file mode 100644 index 00000000..e7662d0c --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/service/MessageService.java @@ -0,0 +1,14 @@ +package com.sprint.mission.discodeit.service; + +import com.sprint.mission.discodeit.entity.Message; + +import java.util.List; +import java.util.UUID; + +public interface MessageService { + Message create(String content, UUID channelId, UUID authorId); + Message find(UUID messageId); + List findAll(); + Message update(UUID messageId, String newContent); + void delete(UUID messageId); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/service/UserService.java b/discodeit/src/main/java/com/sprint/mission/discodeit/service/UserService.java new file mode 100644 index 00000000..e2177193 --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/service/UserService.java @@ -0,0 +1,15 @@ +package com.sprint.mission.discodeit.service; + +import com.sprint.mission.discodeit.entity.User; +import com.sprint.mission.discodeit.record.UserCreateRequest; + +import java.util.List; +import java.util.UUID; + +public interface UserService { + User create(UserCreateRequest request); + User find(UUID userId); + List findAll(); + User update(UUID userId, String newUsername, String newEmail, String newPassword); + void delete(UUID userId); +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicChannelService.java b/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicChannelService.java new file mode 100644 index 00000000..e83ad39d --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicChannelService.java @@ -0,0 +1,51 @@ +package com.sprint.mission.discodeit.service.basic; + +import com.sprint.mission.discodeit.entity.Channel; +import com.sprint.mission.discodeit.entity.ChannelType; +import com.sprint.mission.discodeit.repository.ChannelRepository; +import com.sprint.mission.discodeit.service.ChannelService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.NoSuchElementException; +import java.util.UUID; + +@Service +@RequiredArgsConstructor +public class BasicChannelService implements ChannelService { + private final ChannelRepository channelRepository; + + @Override + public Channel create(ChannelType type, String name, String description) { + Channel channel = new Channel(type, name, description); + return channelRepository.save(channel); + } + + @Override + public Channel find(UUID channelId) { + return channelRepository.findById(channelId) + .orElseThrow(() -> new NoSuchElementException("Channel with id " + channelId + " not found")); + } + + @Override + public List findAll() { + return channelRepository.findAll(); + } + + @Override + public Channel update(UUID channelId, String newName, String newDescription) { + Channel channel = channelRepository.findById(channelId) + .orElseThrow(() -> new NoSuchElementException("Channel with id " + channelId + " not found")); + channel.update(newName, newDescription); + return channelRepository.save(channel); + } + + @Override + public void delete(UUID channelId) { + if (!channelRepository.existsById(channelId)) { + throw new NoSuchElementException("Channel with id " + channelId + " not found"); + } + channelRepository.deleteById(channelId); + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicMessageService.java b/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicMessageService.java new file mode 100644 index 00000000..ca66ed8c --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicMessageService.java @@ -0,0 +1,63 @@ +package com.sprint.mission.discodeit.service.basic; + +import com.sprint.mission.discodeit.entity.Message; +import com.sprint.mission.discodeit.repository.ChannelRepository; +import com.sprint.mission.discodeit.repository.MessageRepository; +import com.sprint.mission.discodeit.repository.UserRepository; +import com.sprint.mission.discodeit.service.MessageService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.UUID; + +@Service +@RequiredArgsConstructor +public class BasicMessageService implements MessageService { + private final MessageRepository messageRepository; + // + private final ChannelRepository channelRepository; + private final UserRepository userRepository; + + @Override + public Message create(String content, UUID channelId, UUID authorId) { + if (!channelRepository.existsById(channelId)) { + throw new NoSuchElementException("Channel not found with id " + channelId); + } + if (!userRepository.existsById(authorId)) { + throw new NoSuchElementException("Author not found with id " + authorId); + } + + Message message = new Message(content, channelId, authorId); + return messageRepository.save(message); + } + + @Override + public Message find(UUID messageId) { + return messageRepository.findById(messageId) + .orElseThrow(() -> new NoSuchElementException("Message with id " + messageId + " not found")); + } + + @Override + public List findAll() { + return messageRepository.findAll(); + } + + @Override + public Message update(UUID messageId, String newContent) { + Message message = messageRepository.findById(messageId) + .orElseThrow(() -> new NoSuchElementException("Message with id " + messageId + " not found")); + message.update(newContent); + return messageRepository.save(message); + } + + @Override + public void delete(UUID messageId) { + if (!messageRepository.existsById(messageId)) { + throw new NoSuchElementException("Message with id " + messageId + " not found"); + } + messageRepository.deleteById(messageId); + } +} diff --git a/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicUserService.java b/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicUserService.java new file mode 100644 index 00000000..082c72fe --- /dev/null +++ b/discodeit/src/main/java/com/sprint/mission/discodeit/service/basic/BasicUserService.java @@ -0,0 +1,64 @@ +package com.sprint.mission.discodeit.service.basic; + +import com.sprint.mission.discodeit.entity.BinaryContent; +import com.sprint.mission.discodeit.entity.User; +import com.sprint.mission.discodeit.record.UserCreateRequest; +import com.sprint.mission.discodeit.repository.UserRepository; +import com.sprint.mission.discodeit.service.UserService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.NoSuchElementException; +import java.util.UUID; + +@Service +@RequiredArgsConstructor +public class BasicUserService implements UserService { + private final UserRepository userRepository; + + @Override + public User create(UserCreateRequest request) { + if (userRepository.existsByUsername(request.userName())) { + throw new IllegalArgumentException("이미 존재하는 이름입니다"); + } + if (userRepository.existsByEmail(request.email())) { + throw new IllegalArgumentException("이미 존재하는 이메일입니다"); + } + + if (request.profileUrl() != null) { + BinaryContent profile = new BinaryContent(request.profileUrl()); + UUID profileUrl = profile.getId(); + } + + User user = new User(username, email, password); + return userRepository.save(user); + } + + @Override + public User find(UUID userId) { + return userRepository.findById(userId) + .orElseThrow(() -> new NoSuchElementException("User with id " + userId + " not found")); + } + + @Override + public List findAll() { + return userRepository.findAll(); + } + + @Override + public User update(UUID userId, String newUsername, String newEmail, String newPassword) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new NoSuchElementException("User with id " + userId + " not found")); + user.update(newUsername, newEmail, newPassword); + return userRepository.save(user); + } + + @Override + public void delete(UUID userId) { + if (!userRepository.existsById(userId)) { + throw new NoSuchElementException("User with id " + userId + " not found"); + } + userRepository.deleteById(userId); + } +} diff --git a/discodeit/src/main/resources/application.yaml b/discodeit/src/main/resources/application.yaml new file mode 100644 index 00000000..8329de47 --- /dev/null +++ b/discodeit/src/main/resources/application.yaml @@ -0,0 +1,3 @@ +spring: + application: + name: discodeit diff --git a/discodeit/src/test/java/com/sprint/mission/discodeit/DiscodeitApplicationTests.java b/discodeit/src/test/java/com/sprint/mission/discodeit/DiscodeitApplicationTests.java new file mode 100644 index 00000000..3a987a21 --- /dev/null +++ b/discodeit/src/test/java/com/sprint/mission/discodeit/DiscodeitApplicationTests.java @@ -0,0 +1,13 @@ +package com.sprint.mission.discodeit; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DiscodeitApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/file-data-map/Channel/09f30df5-336d-435e-acbe-b377fb010f3c.ser b/file-data-map/Channel/09f30df5-336d-435e-acbe-b377fb010f3c.ser new file mode 100644 index 00000000..4e904c27 Binary files /dev/null and b/file-data-map/Channel/09f30df5-336d-435e-acbe-b377fb010f3c.ser differ diff --git a/file-data-map/Channel/4688e63a-3382-4f39-a59f-136fde04e1e5.ser b/file-data-map/Channel/4688e63a-3382-4f39-a59f-136fde04e1e5.ser new file mode 100644 index 00000000..16094f1f Binary files /dev/null and b/file-data-map/Channel/4688e63a-3382-4f39-a59f-136fde04e1e5.ser differ diff --git a/file-data-map/Channel/46a24e5b-a883-497c-b9b9-e80b108f6356.ser b/file-data-map/Channel/46a24e5b-a883-497c-b9b9-e80b108f6356.ser new file mode 100644 index 00000000..56a5eb62 Binary files /dev/null and b/file-data-map/Channel/46a24e5b-a883-497c-b9b9-e80b108f6356.ser differ diff --git a/file-data-map/Channel/4a5cff61-ad8a-4cc3-9199-dc05e016da90.ser b/file-data-map/Channel/4a5cff61-ad8a-4cc3-9199-dc05e016da90.ser new file mode 100644 index 00000000..3ea8e20b Binary files /dev/null and b/file-data-map/Channel/4a5cff61-ad8a-4cc3-9199-dc05e016da90.ser differ diff --git a/file-data-map/Channel/51c791f0-4cb1-47ad-b181-8920a36c09be.ser b/file-data-map/Channel/51c791f0-4cb1-47ad-b181-8920a36c09be.ser new file mode 100644 index 00000000..a6e6f05b Binary files /dev/null and b/file-data-map/Channel/51c791f0-4cb1-47ad-b181-8920a36c09be.ser differ diff --git a/file-data-map/Channel/ff21551a-fe01-4501-8da9-fe17b1840d74.ser b/file-data-map/Channel/ff21551a-fe01-4501-8da9-fe17b1840d74.ser new file mode 100644 index 00000000..6e4b9f55 Binary files /dev/null and b/file-data-map/Channel/ff21551a-fe01-4501-8da9-fe17b1840d74.ser differ diff --git a/file-data-map/Message/1cd74ada-976b-42b0-bc9c-dd8ec534699c.ser b/file-data-map/Message/1cd74ada-976b-42b0-bc9c-dd8ec534699c.ser new file mode 100644 index 00000000..a203cc72 Binary files /dev/null and b/file-data-map/Message/1cd74ada-976b-42b0-bc9c-dd8ec534699c.ser differ diff --git a/file-data-map/Message/214da776-fdae-4a71-87cf-2288d99b8eb6.ser b/file-data-map/Message/214da776-fdae-4a71-87cf-2288d99b8eb6.ser new file mode 100644 index 00000000..fe925ad1 Binary files /dev/null and b/file-data-map/Message/214da776-fdae-4a71-87cf-2288d99b8eb6.ser differ diff --git a/file-data-map/Message/4b21b18c-c2bc-49e9-9521-dea2c8656d82.ser b/file-data-map/Message/4b21b18c-c2bc-49e9-9521-dea2c8656d82.ser new file mode 100644 index 00000000..d6300d3f Binary files /dev/null and b/file-data-map/Message/4b21b18c-c2bc-49e9-9521-dea2c8656d82.ser differ diff --git a/file-data-map/Message/6bf418a8-d182-470c-b513-91ab6954dd96.ser b/file-data-map/Message/6bf418a8-d182-470c-b513-91ab6954dd96.ser new file mode 100644 index 00000000..cd3e4788 Binary files /dev/null and b/file-data-map/Message/6bf418a8-d182-470c-b513-91ab6954dd96.ser differ diff --git a/file-data-map/Message/83744de0-8ce1-4b9e-8f49-d0ebc8d68ece.ser b/file-data-map/Message/83744de0-8ce1-4b9e-8f49-d0ebc8d68ece.ser new file mode 100644 index 00000000..e1ddb88b Binary files /dev/null and b/file-data-map/Message/83744de0-8ce1-4b9e-8f49-d0ebc8d68ece.ser differ diff --git a/file-data-map/Message/e76bf6dc-2cb7-4c5c-bece-c8b1ba839ed1.ser b/file-data-map/Message/e76bf6dc-2cb7-4c5c-bece-c8b1ba839ed1.ser new file mode 100644 index 00000000..ce5af47e Binary files /dev/null and b/file-data-map/Message/e76bf6dc-2cb7-4c5c-bece-c8b1ba839ed1.ser differ diff --git a/file-data-map/User/17ce5928-2488-4286-8f44-9b95858f654b.ser b/file-data-map/User/17ce5928-2488-4286-8f44-9b95858f654b.ser new file mode 100644 index 00000000..4418a58c Binary files /dev/null and b/file-data-map/User/17ce5928-2488-4286-8f44-9b95858f654b.ser differ diff --git a/file-data-map/User/421ad26b-13f5-4dad-a04b-93d53bd354f7.ser b/file-data-map/User/421ad26b-13f5-4dad-a04b-93d53bd354f7.ser new file mode 100644 index 00000000..c779f1a2 Binary files /dev/null and b/file-data-map/User/421ad26b-13f5-4dad-a04b-93d53bd354f7.ser differ diff --git a/file-data-map/User/50a7d29d-ac51-475d-a849-d16ed0c0714c.ser b/file-data-map/User/50a7d29d-ac51-475d-a849-d16ed0c0714c.ser new file mode 100644 index 00000000..1b2d7a8e Binary files /dev/null and b/file-data-map/User/50a7d29d-ac51-475d-a849-d16ed0c0714c.ser differ diff --git a/file-data-map/User/833e604a-7f1e-4b21-88b3-b3f3fee1d428.ser b/file-data-map/User/833e604a-7f1e-4b21-88b3-b3f3fee1d428.ser new file mode 100644 index 00000000..a3733ba4 Binary files /dev/null and b/file-data-map/User/833e604a-7f1e-4b21-88b3-b3f3fee1d428.ser differ diff --git a/file-data-map/User/9b4cc655-1c8e-4686-8bd7-e965b05024e9.ser b/file-data-map/User/9b4cc655-1c8e-4686-8bd7-e965b05024e9.ser new file mode 100644 index 00000000..c60318b1 Binary files /dev/null and b/file-data-map/User/9b4cc655-1c8e-4686-8bd7-e965b05024e9.ser differ diff --git a/file-data-map/User/c6abaaff-fd05-4ecf-80b1-654c4b6d73a7.ser b/file-data-map/User/c6abaaff-fd05-4ecf-80b1-654c4b6d73a7.ser new file mode 100644 index 00000000..79c62fcd Binary files /dev/null and b/file-data-map/User/c6abaaff-fd05-4ecf-80b1-654c4b6d73a7.ser differ diff --git a/lib/lombok-1.18.42.jar b/lib/lombok-1.18.42.jar new file mode 100644 index 00000000..b6b4f023 Binary files /dev/null and b/lib/lombok-1.18.42.jar differ diff --git a/message.dat b/message.dat new file mode 100644 index 00000000..664f42f5 Binary files /dev/null and b/message.dat differ diff --git a/user.dat b/user.dat new file mode 100644 index 00000000..39b11fad Binary files /dev/null and b/user.dat differ