-
Notifications
You must be signed in to change notification settings - Fork 34
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
8 changed files
with
248 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* @devxb |
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
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/org/gitanimals/render/controller/BackgroundController.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,46 @@ | ||
package org.gitanimals.render.controller | ||
|
||
import org.gitanimals.render.app.UserFacade | ||
import org.gitanimals.render.controller.request.ChangeFieldRequest | ||
import org.gitanimals.render.controller.response.BackgroundResponse | ||
import org.gitanimals.render.domain.FieldType | ||
import org.gitanimals.render.domain.UserService | ||
import org.gitanimals.render.domain.UserService.Companion.loadField | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
class BackgroundController( | ||
private val userFacade: UserFacade, | ||
private val userService: UserService, | ||
) { | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("/users/{username}/backgrounds") | ||
fun getBackgrounds( | ||
@PathVariable("username") username: String, | ||
) = BackgroundResponse.from(userService.getByNameWithLazyLoading(username, loadField)) | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@PutMapping("/users/backgrounds") | ||
fun changeBackground( | ||
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String, | ||
@RequestBody changeFieldRequest: ChangeFieldRequest, | ||
) = userFacade.changeField(token, FieldType.valueOf(changeFieldRequest.type.uppercase())) | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@PostMapping("/internals/backgrounds") | ||
fun addBackground( | ||
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String, | ||
@RequestParam(name = "name") name: String, | ||
) = userFacade.addField(token, FieldType.valueOf(name.uppercase())) | ||
|
||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@DeleteMapping("/internals/backgrounds") | ||
fun deleteBackground( | ||
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String, | ||
@RequestParam(name = "name") name: String, | ||
) = userFacade.deleteField(token, FieldType.valueOf(name.uppercase())) | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/org/gitanimals/render/controller/request/ChangeFieldRequest.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 org.gitanimals.render.controller.request | ||
|
||
data class ChangeFieldRequest( | ||
val type: String, | ||
) |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/org/gitanimals/render/controller/response/BackgroundResponse.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 org.gitanimals.render.controller.response | ||
|
||
import org.gitanimals.render.domain.User | ||
|
||
data class BackgroundResponse( | ||
val id: String, | ||
val name: String, | ||
val backgrounds: List<Background>, | ||
) { | ||
|
||
data class Background( | ||
val type: String, | ||
) | ||
|
||
companion object { | ||
fun from(user: User): BackgroundResponse { | ||
return BackgroundResponse( | ||
id = user.id.toString(), | ||
name = user.name, | ||
backgrounds = user.fields.map { Background(it.fieldType.toString()) }, | ||
) | ||
} | ||
} | ||
} |
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,65 @@ | ||
package org.gitanimals.render.domain | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore | ||
import jakarta.persistence.* | ||
import org.gitanimals.render.core.IdGenerator | ||
|
||
@Entity | ||
@Table(name = "field") | ||
class Field( | ||
@Id | ||
@Column(name = "id") | ||
private val id: Long, | ||
|
||
@Column(name = "field_type") | ||
@Enumerated(value = EnumType.STRING) | ||
val fieldType: FieldType, | ||
|
||
@Column(name = "is_choose", nullable = false) | ||
private var isChoose: Boolean, | ||
|
||
@JsonIgnore | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
val user: User, | ||
) { | ||
|
||
fun isChoose(): Boolean = this.isChoose | ||
|
||
fun choose() { | ||
this.isChoose = true | ||
} | ||
|
||
fun unChoose() { | ||
this.isChoose = false | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is Field) return false | ||
|
||
return fieldType == other.fieldType | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return fieldType.hashCode() | ||
} | ||
|
||
fun fillBackground(): String = this.fieldType.fillBackground() | ||
|
||
fun loadComponent(name: String, totalCount: Long): String = | ||
this.fieldType.loadComponent(name, totalCount) | ||
|
||
fun drawBorder(): String = this.fieldType.drawBorder() | ||
|
||
companion object { | ||
fun from(user: User, fieldType: FieldType): Field { | ||
return Field( | ||
id = IdGenerator.generate(), | ||
fieldType = fieldType, | ||
isChoose = false, | ||
user = user, | ||
) | ||
} | ||
} | ||
} |
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