Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/back builder #125

Merged
merged 6 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.goliath.emojihub.springboot.domain.emoji.dao

import com.goliath.emojihub.springboot.domain.emoji.dto.EmojiDto
import com.goliath.emojihub.springboot.domain.emoji.dto.EmojiDtoBuilder
import com.goliath.emojihub.springboot.global.util.StringValue.Bucket.EMOJI_STORAGE_BUCKET_NAME
import com.goliath.emojihub.springboot.global.util.StringValue.Collection.EMOJI_COLLECTION_NAME
import com.goliath.emojihub.springboot.global.util.StringValue.EmojiField.NUM_SAVED
import com.goliath.emojihub.springboot.global.util.StringValue.EmojiField.CREATED_AT
import com.goliath.emojihub.springboot.global.util.generateId
import com.google.cloud.firestore.*
import com.google.cloud.storage.BlobId
import com.google.cloud.storage.BlobInfo
Expand Down Expand Up @@ -71,7 +73,15 @@ class EmojiDao(
.build()
storage.createFrom(thumbnailBlob, ByteArrayInputStream(thumbnail.bytes))
val thumbnailUrl = storage.get(thumbnailBlobId).signUrl(100, TimeUnit.DAYS)
val emoji = EmojiDto(username, emojiUnicode, emojiLabel, emojiVideoUrl.toString(), dateTime, thumbnailUrl.toString())
val emoji = EmojiDtoBuilder()
.id(generateId())
.createdBy(username)
.emojiUnicode(emojiUnicode)
.emojiLabel(emojiLabel)
.videoUrl(emojiVideoUrl.toString())
.createdAt(dateTime)
.thumbnailUrl(thumbnailUrl.toString())
.build()
db.collection(EMOJI_COLLECTION_NAME.string)
.document(emoji.id)
.set(emoji)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.goliath.emojihub.springboot.domain.emoji.dto

import kotlin.streams.asSequence

data class EmojiDto(
var id: String = "",
var created_by: String = "",
Expand All @@ -11,21 +9,52 @@ data class EmojiDto(
var created_at: String = "",
var num_saved: Int = 0,
var thumbnail_url: String = ""
){
constructor(username: String, emojiUnicode: String, emojiLabel: String, emojiVideoUrl: String, dateTime: String, thumbnailUrl: String) : this() {
val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
val outputStrLength: Long = 20
id = java.util.Random().ints(outputStrLength, 0, source.length)
.asSequence()
.map(source::get)
.joinToString("")

created_by = username
video_url = emojiVideoUrl
emoji_unicode = emojiUnicode
emoji_label = emojiLabel
created_at = dateTime
num_saved = 0
thumbnail_url = thumbnailUrl
)

class EmojiDtoBuilder {
private val emojiDto: EmojiDto = EmojiDto()

fun id(id: String): EmojiDtoBuilder {
emojiDto.id = id
return this
}

fun createdBy(createdBy: String): EmojiDtoBuilder {
emojiDto.created_by = createdBy
return this
}

fun videoUrl(videoUrl: String): EmojiDtoBuilder {
emojiDto.video_url = videoUrl
return this
}

fun emojiUnicode(emojiUnicode: String): EmojiDtoBuilder {
emojiDto.emoji_unicode = emojiUnicode
return this
}

fun emojiLabel(emojiLabel: String): EmojiDtoBuilder {
emojiDto.emoji_label = emojiLabel
return this
}

fun createdAt(createdAt: String): EmojiDtoBuilder {
emojiDto.created_at = createdAt
return this
}

fun numSaved(numSaved: Int): EmojiDtoBuilder {
emojiDto.num_saved = numSaved
return this
}

fun thumbnailUrl(thumbnailUrl: String): EmojiDtoBuilder {
emojiDto.thumbnail_url = thumbnailUrl
return this
}

fun build(): EmojiDto {
return emojiDto
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,21 @@ class EmojiService(
} else {
user.saved_emojis
}
val emojiArrayDeque = ArrayDeque<EmojiDto>()
var emojiList = listOf<EmojiDto>()
if (emojiIdList != null && emojiIdList.size != 0) {
var emojiList = mutableListOf<EmojiDto>()
if (emojiIdList.size != 0) {
for (emojiId in emojiIdList) {
val emoji = emojiDao.getEmoji(emojiId) ?: continue
emojiArrayDeque.addFirst(emoji)
emojiList.add(emoji)
}
// sort
if (emojiList.size != 0) {
emojiList.reverse()
// pagination
emojiList = emojiList.subList(
min((index - 1) * count, emojiList.size - 1),
min(index * count, emojiList.size)
)
}
// pagination
// FIXME: remind - CreatedEmojiList should already be sorted by created_at
val start = min((index - 1) * count, emojiArrayDeque.size - 1)
val end = min(index * count, emojiArrayDeque.size)
emojiList = emojiArrayDeque.toList().subList(start, end)
}
return emojiList
}
Expand All @@ -88,9 +91,9 @@ class EmojiService(
if (emojiDao.existsEmoji(emojiId).not())
throw CustomHttp404(EMOJI_NOT_FOUND)
val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND)
if (user.created_emojis?.contains(emojiId) == true)
if (user.created_emojis.contains(emojiId))
throw CustomHttp403(USER_CREATED)
if (user.saved_emojis?.contains(emojiId) == true)
if (user.saved_emojis.contains(emojiId))
throw CustomHttp403(USER_ALREADY_SAVED)
emojiDao.numSavedChange(emojiId, 1)
userDao.insertId(username, emojiId, SAVED_EMOJIS.string)
Expand All @@ -100,9 +103,9 @@ class EmojiService(
if (emojiDao.existsEmoji(emojiId).not())
throw CustomHttp404(EMOJI_NOT_FOUND)
val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND)
if (user.created_emojis?.contains(emojiId) == true)
if (user.created_emojis.contains(emojiId))
throw CustomHttp403(USER_CREATED)
if (user.saved_emojis == null || !user.saved_emojis!!.contains(emojiId))
if (!user.saved_emojis.contains(emojiId))
throw CustomHttp403(USER_ALREADY_UNSAVED)
emojiDao.numSavedChange(emojiId, -1)
userDao.deleteId(username, emojiId, SAVED_EMOJIS.string)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.goliath.emojihub.springboot.domain.post.dao

import com.goliath.emojihub.springboot.domain.post.dto.PostDto
import com.goliath.emojihub.springboot.domain.post.dto.PostDtoBuilder
import com.goliath.emojihub.springboot.domain.post.dto.ReactionWithEmojiUnicode
import com.goliath.emojihub.springboot.global.util.StringValue.Collection.POST_COLLECTION_NAME
import com.goliath.emojihub.springboot.global.util.StringValue.PostField.CONTENT
import com.goliath.emojihub.springboot.global.util.StringValue.PostField.CREATED_AT
import com.goliath.emojihub.springboot.global.util.StringValue.PostField.MODIFIED_AT
import com.goliath.emojihub.springboot.global.util.StringValue.PostField.REACTIONS
import com.goliath.emojihub.springboot.global.util.generateId
import com.goliath.emojihub.springboot.global.util.getDateTimeNow
import com.google.cloud.firestore.*
import lombok.extern.slf4j.Slf4j
Expand All @@ -20,7 +22,12 @@ class PostDao(

fun insertPost(username: String, content: String): PostDto {
val dateTime = getDateTimeNow()
val post = PostDto(username, content, dateTime)
val post = PostDtoBuilder()
.id(generateId())
.createdBy(username)
.content(content)
.createdAt(dateTime)
.build()
db.collection(POST_COLLECTION_NAME.string)
.document(post.id)
.set(post)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
package com.goliath.emojihub.springboot.domain.post.dto

import kotlin.streams.asSequence

data class PostDto(
var id: String = "",
var created_by: String = "",
var content: String = "",
var created_at: String = "",
var modified_at: String = "",
var reactions: MutableList<ReactionWithEmojiUnicode> = mutableListOf(),
) {
constructor(username: String, content: String, dateTime: String) : this() {
val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
val outputStrLength: Long = 20
id = java.util.Random().ints(outputStrLength, 0, source.length)
.asSequence()
.map(source::get)
.joinToString("")
created_by = username
this.content = content
created_at = dateTime
modified_at = dateTime
}
}
)

data class ReactionWithEmojiUnicode(
var id: String = "",
var emoji_unicode: String = ""
)
)

class PostDtoBuilder {
private val postDto: PostDto = PostDto()

fun id(id: String): PostDtoBuilder {
postDto.id = id
return this
}

fun createdBy(createdBy: String): PostDtoBuilder {
postDto.created_by = createdBy
return this
}

fun content(content: String): PostDtoBuilder {
postDto.content = content
return this
}

fun createdAt(createdAt: String): PostDtoBuilder {
postDto.created_at = createdAt
return this
}

fun modifiedAt(modifiedAt: String): PostDtoBuilder {
postDto.modified_at = modifiedAt
return this
}

fun build(): PostDto {
return postDto
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.goliath.emojihub.springboot.domain.reaction.dao

import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDto
import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDtoBuilder
import com.goliath.emojihub.springboot.global.util.StringValue.Collection.REACTION_COLLECTION_NAME
import com.goliath.emojihub.springboot.global.util.StringValue.ReactionField.CREATED_BY
import com.goliath.emojihub.springboot.global.util.StringValue.ReactionField.POST_ID
import com.goliath.emojihub.springboot.global.util.StringValue.ReactionField.EMOJI_ID
import com.goliath.emojihub.springboot.global.util.StringValue.ReactionField.CREATED_AT
import com.goliath.emojihub.springboot.global.util.generateId
import com.goliath.emojihub.springboot.global.util.getDateTimeNow
import com.google.cloud.firestore.DocumentSnapshot
import com.google.cloud.firestore.Firestore
Expand All @@ -21,7 +23,13 @@ class ReactionDao(

fun insertReaction(username: String, postId: String, emojiId: String): ReactionDto {
val dateTime = getDateTimeNow()
val reaction = ReactionDto(username, postId, emojiId, dateTime)
val reaction = ReactionDtoBuilder()
.id(generateId())
.createdBy(username)
.postId(postId)
.emojiId(emojiId)
.createdAt(dateTime)
.build()
db.collection(REACTION_COLLECTION_NAME.string)
.document(reaction.id)
.set(reaction)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
package com.goliath.emojihub.springboot.domain.reaction.dto

import kotlin.streams.asSequence

data class ReactionDto(
var id: String = "",
var created_by: String = "",
var post_id: String = "",
var emoji_id: String = "",
var created_at: String = ""
) {
constructor(username: String, post_id: String, emoji_id: String, dateTime: String) : this() {
val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
val outputStrLength: Long = 20
id = java.util.Random().ints(outputStrLength, 0, source.length)
.asSequence()
.map(source::get)
.joinToString("")
created_by = username
this.post_id = post_id
this.emoji_id = emoji_id
created_at = dateTime
)

class ReactionDtoBuilder {
private val reactionDto: ReactionDto = ReactionDto()

fun id(id: String): ReactionDtoBuilder {
reactionDto.id = id
return this
}

fun createdBy(createdBy: String): ReactionDtoBuilder {
reactionDto.created_by = createdBy
return this
}

fun postId(postId: String): ReactionDtoBuilder {
reactionDto.post_id = postId
return this
}

fun emojiId(emojiId: String): ReactionDtoBuilder {
reactionDto.emoji_id = emojiId
return this
}

fun createdAt(createdAt: String): ReactionDtoBuilder {
reactionDto.created_at = createdAt
return this
}

fun build(): ReactionDto {
return reactionDto
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@ data class UserDto (
var email: String = "",
var username: String = "",
var password: String = "",
var created_emojis: MutableList<String>? = mutableListOf(),
var saved_emojis: MutableList<String>? = mutableListOf(),
var created_posts: MutableList<String>? = mutableListOf(),
var created_emojis: MutableList<String> = mutableListOf(),
var saved_emojis: MutableList<String> = mutableListOf(),
var created_posts: MutableList<String> = mutableListOf(),
) {
data class AuthToken(
val accessToken: String,
)
}

class UserDtoBuilder {
private val userDto: UserDto = UserDto()

fun email(email: String): UserDtoBuilder {
userDto.email = email
return this
}

fun username(username: String): UserDtoBuilder {
userDto.username = username
return this
}

fun password(password: String): UserDtoBuilder {
userDto.password = password
return this
}

fun build(): UserDto {
return userDto
}
}
Loading
Loading