-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ Merge pull request #27 from juwon-code/main
[Feat] ํผ๋ ์ปดํฌ๋ํธ ๋ง์ด๊ทธ๋ ์ด์ ๋ฐ ๋ฆฌํฉํ ๋ง ์๋ฃ
- Loading branch information
Showing
32 changed files
with
475 additions
and
385 deletions.
There are no files selected for viewing
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
3 changes: 2 additions & 1 deletion
3
src/main/kotlin/org/tenten/bittakotlin/chat/service/ChatService.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
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
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/org/tenten/bittakotlin/feed/constant/FeedError.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,8 @@ | ||
package org.tenten.bittakotlin.feed.constant | ||
|
||
enum class FeedError(val code: Int, val message: String) { | ||
NOT_FOUND(404, "ํผ๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค."), | ||
CANNOT_FOUND(404, "ํผ๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค."), | ||
CANNOT_MODIFY_BAD_AUTHORITY(403, "ํผ๋๋ฅผ ์์ ํ ๊ถํ์ด ์์ต๋๋ค."), | ||
CANNOT_DELETE_BAD_AUTHORITY(403, "ํผ๋๋ฅผ ์ญ์ ํ ๊ถํ์ด ์์ต๋๋ค."), | ||
} |
120 changes: 43 additions & 77 deletions
120
src/main/kotlin/org/tenten/bittakotlin/feed/controller/FeedController.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,108 +1,74 @@ | ||
package org.tenten.bittakotlin.feed.controller | ||
|
||
import org.tenten.bittakotlin.feed.dto.FeedDTO | ||
import org.tenten.bittakotlin.feed.dto.FeedRequestDto.Modify | ||
import org.tenten.bittakotlin.feed.service.FeedService | ||
import org.tenten.bittakotlin.global.constants.ApiResponses.* | ||
import org.tenten.bittakotlin.global.exception.AuthenticationException | ||
import org.tenten.bittakotlin.global.util.AuthenticationProvider | ||
import org.tenten.bittakotlin.member.entity.Role | ||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.Parameter | ||
import io.swagger.v3.oas.annotations.Parameters | ||
import io.swagger.v3.oas.annotations.media.Content | ||
import io.swagger.v3.oas.annotations.media.Schema | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import jakarta.validation.Valid | ||
import jakarta.validation.constraints.Min | ||
import lombok.RequiredArgsConstructor | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.validation.annotation.Validated | ||
import org.springframework.web.bind.annotation.* | ||
import org.springframework.web.multipart.MultipartFile | ||
import java.util.Map | ||
import org.tenten.bittakotlin.feed.dto.FeedRequestDto | ||
import org.tenten.bittakotlin.security.service.PrincipalProvider | ||
|
||
@Tag(name = "ํผ๋ API ์ปจํธ๋กค๋ฌ", description = "ํผ๋์ ๊ด๋ จ๋ REST API๋ฅผ ์ ๊ณตํ๋ ์ปจํ๋กค๋ฌ์ ๋๋ค.") | ||
@RestController | ||
@RequestMapping("/api/v1/feed") | ||
@RequiredArgsConstructor | ||
@Validated | ||
class FeedController { | ||
private val feedService: FeedService? = null | ||
|
||
|
||
class FeedController ( | ||
private val feedService: FeedService | ||
) { | ||
@GetMapping | ||
fun getFeeds( | ||
fun readAll( | ||
@RequestParam(required = false, defaultValue = "0", value = "page") page: Int, | ||
@RequestParam(required = false, defaultValue = "10", value = "size") size: Int, | ||
@RequestParam(required = false, value = "username") username: String?, | ||
@RequestParam(required = false, value = "nickname") nickname: String?, | ||
@RequestParam(required = false, value = "title") title: String? | ||
): ResponseEntity<*> { | ||
): ResponseEntity<Map<String, Any>> { | ||
val pageable: Pageable = PageRequest.of(page, size) | ||
|
||
return ResponseEntity.ok<T>( | ||
Map.of<K, V>( | ||
"message", "ํผ๋๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์กฐํํ์ต๋๋ค.", | ||
"result", feedService.readAll(pageable, username, title) | ||
) | ||
) | ||
return ResponseEntity.ok(mapOf( | ||
"message" to "ํผ๋ ๋ชฉ๋ก์ ์ฑ๊ณต์ ์ผ๋ก ์กฐํํ์ต๋๋ค.", | ||
"result" to feedService.getAll(pageable, nickname, title) | ||
)) | ||
} | ||
|
||
@GetMapping("/{id}") | ||
fun getFeedById(@PathVariable("id") id: @Min(1) Long?): ResponseEntity<*> { | ||
return ResponseEntity.ok<T>( | ||
Map.of<K, V>("message", "ํผ๋๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์กฐํํ์ต๋๋ค.", "result", feedService.read(id)) | ||
) | ||
@GetMapping("/random") | ||
fun readRandom(@RequestParam(required = false, defaultValue = "0", value = "page") size: Int | ||
): ResponseEntity<Map<String, Any>> { | ||
return ResponseEntity.ok(mapOf( | ||
"message" to "ํผ๋ ๋ชฉ๋ก์ ๋ฌด์์๋ก ์กฐํํ์ต๋๋ค.", | ||
"result" to feedService.getRandom(size) | ||
)) | ||
} | ||
|
||
@PostMapping(consumes = [MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE]) | ||
fun createFeed( | ||
@RequestPart(value = "feed") feedDto: @Valid FeedDTO?, | ||
@RequestPart(value = "files", required = false) files: List<MultipartFile?>? | ||
): ResponseEntity<*> { | ||
feedService.insert(feedDto, files) | ||
@GetMapping("/{id}") | ||
fun read(@PathVariable("id") id: Long): ResponseEntity<Map<String, Any>> { | ||
return ResponseEntity.ok(mapOf( | ||
"message" to "ํผ๋๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์กฐํํ์ต๋๋ค.", | ||
"result" to feedService.get(id) | ||
)) | ||
} | ||
|
||
return ResponseEntity.ok().body(Map.of("message", "ํผ๋๊ฐ ๋ฑ๋ก๋์์ต๋๋ค.")) | ||
@PostMapping | ||
fun create(requestDto: FeedRequestDto.Create): ResponseEntity<Map<String, Any>> { | ||
return ResponseEntity.ok(mapOf( | ||
"message" to "ํผ๋๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ๋ฑ๋กํ์ต๋๋ค.", | ||
"result" to feedService.save(requestDto) | ||
)) | ||
} | ||
|
||
@PutMapping(value = ["/{id}"], consumes = [MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE]) | ||
@PutMapping | ||
fun modifyFeed( | ||
@PathVariable("id") id: @Min(1) Long?, | ||
@RequestPart("feed") feedDTO: @Valid Modify, | ||
@RequestPart("filesToUpload") filesToUpload: List<MultipartFile?>?, | ||
@RequestPart("filesToDelete") filesToDelete: List<String?>? | ||
): ResponseEntity<*> { | ||
if (!checkPermission(id)) { | ||
throw AuthenticationException.CANNOT_ACCESS.get() | ||
} | ||
|
||
feedDTO.setId(id) | ||
|
||
feedService.update(feedDTO, filesToUpload, filesToDelete) | ||
|
||
return ResponseEntity.ok().body(Map.of("message", "ํผ๋๊ฐ ์์ ๋์์ต๋๋ค.")) | ||
@PathVariable("id") id: Long, @RequestBody requestDto: FeedRequestDto.Modify): | ||
ResponseEntity<Map<String, Any>> { | ||
return ResponseEntity.ok(mapOf( | ||
"message" to "ํผ๋๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์์ ํ์ต๋๋ค.", | ||
"result" to feedService.update(id, requestDto) | ||
)) | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
fun deleteFeed(@PathVariable("id") id: @Min(1) Long?): ResponseEntity<*> { | ||
if (!checkPermission(id)) { | ||
throw AuthenticationException.CANNOT_ACCESS.get() | ||
} | ||
|
||
fun deleteFeed(@PathVariable id: Long): ResponseEntity<Map<String, Any>> { | ||
feedService.delete(id) | ||
|
||
return ResponseEntity.ok().body(Map.of("message", "ํผ๋๊ฐ ์ญ์ ๋์์ต๋๋ค.")) | ||
} | ||
|
||
private fun checkPermission(id: Long?): Boolean { | ||
if (AuthenticationProvider.getRoles() === Role.ROLE_ADMIN) { | ||
return true | ||
} | ||
|
||
return feedService.checkAuthority(id, AuthenticationProvider.getUsername()) | ||
return ResponseEntity.ok(mapOf( | ||
"message" to "ํผ๋๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์ญ์ ํ์ต๋๋ค." | ||
)) | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
src/main/kotlin/org/tenten/bittakotlin/feed/dto/FeedDTO.kt
This file was deleted.
Oops, something went wrong.
46 changes: 18 additions & 28 deletions
46
src/main/kotlin/org/tenten/bittakotlin/feed/dto/FeedRequestDto.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,33 +1,23 @@ | ||
package org.tenten.bittakotlin.feed.dto | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema | ||
import jakarta.validation.constraints.Min | ||
import jakarta.validation.constraints.NotBlank | ||
import jakarta.validation.constraints.NotNull | ||
import jakarta.validation.constraints.Size | ||
import lombok.AllArgsConstructor | ||
import lombok.Builder | ||
import lombok.Data | ||
import lombok.NoArgsConstructor | ||
import org.tenten.bittakotlin.media.dto.MediaRequestDto | ||
|
||
class FeedRequestDto { | ||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
class Modify { | ||
@Schema(title = "ํผ๋ ID (PK)", description = "ํผ๋์ ๊ณ ์ ID ์ ๋๋ค.", example = "1", minimum = "1") | ||
val id: @Min(value = 1, message = "ID๋ 0 ๋๋ ์์๊ฐ ๋ ์ ์์ต๋๋ค.") Long? = null | ||
|
||
@Schema(title = "ํผ๋ ์ ๋ชฉ", description = "ํผ๋ ์ ๋ชฉ์ ๋๋ค.", example = "Feed Title", minimum = "1", maximum = "50") | ||
val title: @NotBlank(message = "์ ๋ชฉ์ ๋น์ฐ๊ฑฐ๋, ๊ณต๋ฐฑ์ด ๋ ์ ์์ต๋๋ค.") @Size( | ||
min = 1, | ||
max = 50, | ||
message = "์ ๋ชฉ์ 1 ~ 50์ ์ดํ์ฌ์ผ ํฉ๋๋ค." | ||
) String? = null | ||
|
||
@Schema(title = "ํผ๋ ๋ด์ฉ", description = "ํผ๋ ๋ด์ฉ์ ๋๋ค.", example = "Feed Content") | ||
@Builder.Default | ||
val content: @NotNull String = "" | ||
} | ||
data class Create ( | ||
val title: String, | ||
|
||
val content: String, | ||
|
||
val medias: List<MediaRequestDto.Upload>? | ||
) | ||
|
||
data class Modify ( | ||
val title: String, | ||
|
||
val content: String, | ||
|
||
val uploads: List<MediaRequestDto.Upload>?, | ||
|
||
val deletes: List<MediaRequestDto.Delete>? | ||
) | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/org/tenten/bittakotlin/feed/dto/FeedResponseDto.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 org.tenten.bittakotlin.feed.dto | ||
|
||
import org.tenten.bittakotlin.media.dto.MediaResponseDto | ||
import java.time.LocalDateTime | ||
|
||
class FeedResponseDto { | ||
data class Read ( | ||
val id: Long, | ||
|
||
val title: String, | ||
|
||
val content: String, | ||
|
||
val author: String, | ||
|
||
val createdAt: LocalDateTime, | ||
|
||
val medias: List<MediaResponseDto.Read> | ||
) | ||
|
||
data class Create ( | ||
val medias: List<MediaResponseDto.Read> | ||
) | ||
|
||
data class Modify ( | ||
val medias: List<MediaResponseDto.Read> | ||
) | ||
} |
Oops, something went wrong.