-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
331 additions
and
151 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/toyProject7/karrot/article/ArticleException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 74 additions & 20 deletions
94
src/main/kotlin/com/toyProject7/karrot/article/controller/ArticleController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleLikesEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleLikesRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
15 changes: 12 additions & 3 deletions
15
src/main/kotlin/com/toyProject7/karrot/article/persistence/ArticleRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
Oops, something went wrong.