Skip to content

Commit

Permalink
🔀 Merge main to feat/mypage
Browse files Browse the repository at this point in the history
  • Loading branch information
wonseok committed Jan 7, 2025
2 parents 9dece62 + f9b2361 commit b3203ac
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 151 deletions.
24 changes: 24 additions & 0 deletions src/main/kotlin/com/toyProject7/karrot/article/ArticleException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.toyProject7.karrot.article

import com.toyProject7.karrot.DomainException
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode

sealed class ArticleException(
errorCode: Int,
httpStatusCode: HttpStatusCode,
msg: String,
cause: Throwable? = null,
) : DomainException(errorCode, httpStatusCode, msg, cause)

class ArticleNotFoundException : ArticleException(
errorCode = 0,
httpStatusCode = HttpStatus.NOT_FOUND,
msg = "Article not found",
)

class ArticlePermissionDeniedException : ArticleException(
errorCode = 0,
httpStatusCode = HttpStatus.UNAUTHORIZED,
msg = "Permission denied",
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import com.toyProject7.karrot.user.controller.User
import java.time.Instant

data class Article(
val id: String,
val id: Long,
val seller: User,
val buyer: User?,
val title: String,
val content: String,
val price: Int,
val isSelled: Boolean,
val status: String,
val location: String,
val createdAt: Instant,
val likeCount: Int,
Expand All @@ -21,11 +20,10 @@ data class Article(
return Article(
id = entity.id!!,
seller = User.fromEntity(entity.seller),
buyer = entity.buyer?.let { User.fromEntity(it) },
title = entity.title,
content = entity.content,
price = entity.price,
isSelled = entity.isSelled,
status = entity.status,
location = entity.location,
createdAt = entity.createdAt,
likeCount = entity.articleLikes.size,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,100 @@
package com.toyProject7.karrot.article.controller

import com.toyProject7.karrot.article.persistence.ArticleEntity
import com.toyProject7.karrot.article.service.ArticleService
import com.toyProject7.karrot.user.AuthUser
import com.toyProject7.karrot.user.controller.User
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
class ArticleController(
private val articleService: ArticleService,
) {
@GetMapping("/api/mypage/likes")
fun getLikedArticles(
@PostMapping("api/item/post")
fun postArticle(
@RequestBody request: PostArticleRequest,
@AuthUser user: User,
): ResponseEntity<LikedItemsResponse> {
val articles = articleService.listLikedArticles(user.userId)
return ResponseEntity.ok(articles)
): ResponseEntity<Article> {
val article = articleService.postArticle(request, user.id)
return ResponseEntity.ok(article)
}

@PutMapping("api/item/edit/{articleId}")
fun editArticle(
@RequestBody request: PostArticleRequest,
@PathVariable articleId: Long,
@AuthUser user: User,
): ResponseEntity<Article> {
val article = articleService.editArticle(articleId, request, user.id)
return ResponseEntity.ok(article)
}

@DeleteMapping("api/item/delete/{articleId}")
fun deleteArticle(
@PathVariable articleId: Long,
@AuthUser user: User,
): ResponseEntity<String> {
articleService.deleteArticle(articleId, user.id)
return ResponseEntity.ok("Deleted Successfully")
}

@GetMapping("api/item/get/{articleId}")
fun getArticle(
@PathVariable articleId: Long,
): ResponseEntity<Article> {
return ResponseEntity.ok(articleService.getArticle(articleId))
}

@GetMapping("/api/home")
fun getPreviousArticles(
@RequestParam("articleId") articleId: Long,
): ResponseEntity<List<Item>> {
val articles: List<ArticleEntity> = articleService.getPreviousArticles(articleId)
val response: List<Item> =
articles.map { article ->
Item.fromArticle(Article.fromEntity(article))
}
return ResponseEntity.ok(response)
}

@GetMapping("/api/mypage/sells")
fun getSellingAndSoldArticles(
fun getArticlesBySeller(
@RequestParam("articleId") articleId: Long,
@AuthUser user: User,
): ResponseEntity<SellingAndSoldItemsResponse> {
val articles = articleService.listSellingAndSoldArticles(user.userId)
return ResponseEntity.ok(articles)
): ResponseEntity<List<Item>> {
val articles: List<ArticleEntity> = articleService.getArticlesBySeller(user.id, articleId)
val response: List<Item> =
articles.map { article ->
Item.fromArticle(Article.fromEntity(article))
}
return ResponseEntity.ok(response)
}

@GetMapping("/api/mypage/buys")
fun getBoughtArticles(
fun getArticlesByBuyer(
@RequestParam("articleId") articleId: Long,
@AuthUser user: User,
): ResponseEntity<BoughtItemsResponse> {
val articles = articleService.listBoughtArticles(user.userId)
return ResponseEntity.ok(articles)
): ResponseEntity<List<Item>> {
val articles = articleService.getArticlesByBuyer(user.id, articleId)
val response: List<Item> =
articles.map { article ->
Item.fromArticle(Article.fromEntity(article))
}
return ResponseEntity.ok(response)
}
}

typealias LikedItemsResponse = List<Item>

data class SellingAndSoldItemsResponse(
val sellingItems: List<Item>,
val soldItems: List<Item>,
data class PostArticleRequest(
val title: String,
val content: String,
val price: Int,
val location: String,
)

typealias BoughtItemsResponse = List<Item>
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ package com.toyProject7.karrot.article.controller

import java.time.Instant

// article's preview information
data class Item(
val id: String,
val id: Long,
val title: String,
val price: Int,
val status: String,
val location: String,
val createdAt: Instant,
val price: Int,
val likeCount: Int,
) {
companion object {
fun fromArticle(article: Article): Item {
return Item(
id = article.id,
title = article.title,
price = article.price,
status = article.status,
location = article.location,
createdAt = article.createdAt,
price = article.price,
likeCount = article.likeCount,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@ import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToMany
import jakarta.persistence.OneToOne
import java.time.Instant

@Entity(name = "articles")
@Entity(name = "article")
class ArticleEntity(
@Id
@GeneratedValue(strategy = GenerationType.UUID)
val id: String? = null,
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@ManyToOne
@JoinColumn(name = "seller_id")
@JoinColumn(name = "seller")
var seller: UserEntity,
@OneToOne
@JoinColumn(name = "buyer_id")
var buyer: UserEntity? = null,
@Column(columnDefinition = "TEXT")
@ManyToOne
@JoinColumn(name = "buyer")
var buyer: UserEntity?,
@Column(name = "title", nullable = false)
var title: String,
@Column(columnDefinition = "TEXT")
@Column(name = "content", nullable = false)
var content: String,
@Column
@Column(name = "price", nullable = false)
var price: Int,
@Column(name = "is_selled")
var isSelled: Boolean,
@Column
@Column(name = "status", nullable = false)
var status: String,
@Column(name = "location", nullable = false)
var location: String,
@OneToMany(mappedBy = "article")
var articleLikes: MutableList<ArticleLikeEntity> = mutableListOf(),
var articleLikes: MutableList<ArticleLikesEntity> = mutableListOf(),
@Column(name = "created_at", nullable = false)
var createdAt: Instant,
@Column(name = "updated_at", nullable = false)
var updatedAt: Instant,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.toyProject7.karrot.article.persistence

import com.toyProject7.karrot.user.persistence.UserEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import java.time.Instant

@Entity(name = "article_likes")
class ArticleLikesEntity(
@Id
@GeneratedValue(strategy = GenerationType.UUID)
val id: String? = null,
@ManyToOne
@JoinColumn(name = "article_id")
var article: ArticleEntity,
@ManyToOne
@JoinColumn(name = "user")
var user: UserEntity,
@Column(name = "created_at", nullable = false)
var createdAt: Instant,
@Column(name = "updated_at", nullable = false)
var updatedAt: Instant,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.toyProject7.karrot.article.persistence

import org.springframework.data.jpa.repository.JpaRepository

interface ArticleLikesRepository : JpaRepository<ArticleLikesEntity, String>
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package com.toyProject7.karrot.article.persistence

import com.toyProject7.karrot.user.persistence.UserEntity
import org.springframework.data.jpa.repository.JpaRepository

interface ArticleRepository : JpaRepository<ArticleEntity, String> {
fun findAllByBuyerId(userId: String): List<ArticleEntity>
interface ArticleRepository : JpaRepository<ArticleEntity, Long> {
fun findTop10ByIdBeforeOrderByCreatedAtDesc(id: Long): List<ArticleEntity>

fun findAllBySellerId(userId: String): List<ArticleEntity>
fun findTop10BySellerAndIdLessThanOrderByIdDesc(
seller: UserEntity,
id: Long,
): List<ArticleEntity>

fun findTop10ByBuyerAndIdLessThanOrderByIdDesc(
buyer: UserEntity,
id: Long,
): List<ArticleEntity>
}
Loading

0 comments on commit b3203ac

Please sign in to comment.