Skip to content

Commit

Permalink
Merge pull request #88 from Sports-day/feature/sport-reference-to-tea…
Browse files Browse the repository at this point in the history
…m-tag

TeamTagにSportを外部キーとして登録
  • Loading branch information
testusuke authored May 9, 2024
2 parents f27aa7b + e2cd2ce commit d22b9e2
Show file tree
Hide file tree
Showing 11 changed files with 876 additions and 762 deletions.
2 changes: 1 addition & 1 deletion dbdoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
| [roles](roles.md) | 6 | | BASE TABLE |
| [sports](sports.md) | 9 | | BASE TABLE |
| [tags](tags.md) | 5 | | BASE TABLE |
| [team_tags](team_tags.md) | 4 | | BASE TABLE |
| [team_tags](team_tags.md) | 5 | | BASE TABLE |
| [team_users](team_users.md) | 2 | | BASE TABLE |
| [teams](teams.md) | 7 | | BASE TABLE |
| [tournament_path](tournament_path.md) | 2 | | BASE TABLE |
Expand Down
2 changes: 1 addition & 1 deletion dbdoc/schema.json

Large diffs are not rendered by default.

964 changes: 487 additions & 477 deletions dbdoc/schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dbdoc/sports.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CREATE TABLE `sports` (

| Name | Type | Default | Nullable | Extra Definition | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | ---------------- | -------- | ------- | ------- |
| id | int | | false | auto_increment | [games](games.md) [matches](matches.md) | | |
| id | int | | false | auto_increment | [games](games.md) [matches](matches.md) [team_tags](team_tags.md) | | |
| name | varchar(64) | | false | | | | |
| description | text | | false | | | | |
| icon_image | int | | true | | | [images](images.md) | |
Expand Down
373 changes: 202 additions & 171 deletions dbdoc/sports.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion dbdoc/team_tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
CREATE TABLE `team_tags` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`sport` int DEFAULT NULL,
`created_at` datetime(6) NOT NULL,
`updated_at` datetime(6) NOT NULL,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
KEY `fk_team_tags_sport__id` (`sport`),
CONSTRAINT `fk_team_tags_sport__id` FOREIGN KEY (`sport`) REFERENCES `sports` (`id`) ON DELETE SET NULL ON UPDATE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=[Redacted by tbls] DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

Expand All @@ -23,19 +26,22 @@ CREATE TABLE `team_tags` (
| ---- | ---- | ------- | -------- | ---------------- | -------- | ------- | ------- |
| id | int | | false | auto_increment | [teams](teams.md) | | |
| name | varchar(64) | | false | | | | |
| sport | int | | true | | | [sports](sports.md) | |
| created_at | datetime(6) | | false | | | | |
| updated_at | datetime(6) | | false | | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| fk_team_tags_sport__id | FOREIGN KEY | FOREIGN KEY (sport) REFERENCES sports (id) |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| fk_team_tags_sport__id | KEY fk_team_tags_sport__id (sport) USING BTREE |
| PRIMARY | PRIMARY KEY (id) USING BTREE |

## Relations
Expand Down
148 changes: 97 additions & 51 deletions dbdoc/team_tags.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 22 additions & 19 deletions dbdoc/teams.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 42 additions & 40 deletions src/main/kotlin/net/sportsday/bin/Seeder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,57 @@ fun main() {
},
)

// tag
val tags = listOf(
TagEntity.new {
name = "晴天時"
enabled = true
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
TagEntity.new {
name = "雨天時"
enabled = true
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
)

// sports
val sports = listOf(
SportEntity.new {
name = "バスケットボール晴天時"
description = "バスケットボール晴天時"
iconImage = null
weight = 10
ruleId = 1
tag = tags[0]
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
SportEntity.new {
name = "バスケットボール雨天時"
description = "バスケットボール雨天時"
iconImage = null
weight = 5
ruleId = 1
tag = tags[1]
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
)

// TeamTag
val teamTags = listOf(
TeamTagEntity.new {
name = "バスケットボール晴天時"
sport = sports[0]
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
TeamTagEntity.new {
name = "バスケットボール雨天時"
sport = sports[1]
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
}
Expand Down Expand Up @@ -265,46 +307,6 @@ fun main() {
},
)

// tag
val tags = listOf(
TagEntity.new {
name = "晴天時"
enabled = true
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
TagEntity.new {
name = "雨天時"
enabled = true
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
)

// sports
val sports = listOf(
SportEntity.new {
name = "バスケットボール晴天時"
description = "バスケットボール晴天時"
iconImage = null
weight = 10
ruleId = 1
tag = tags[0]
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
SportEntity.new {
name = "バスケットボール雨天時"
description = "バスケットボール雨天時"
iconImage = null
weight = 5
ruleId = 1
tag = tags[1]
createdAt = LocalDateTime.now()
updatedAt = LocalDateTime.now()
},
)

// game
val games = listOf(
GameEntity.new {
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/net/sportsday/models/TeamTag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.ReferenceOption
import org.jetbrains.exposed.sql.javatime.datetime

/**
Expand All @@ -14,6 +15,7 @@ import org.jetbrains.exposed.sql.javatime.datetime

object TeamTags : IntIdTable("team_tags") {
val name = varchar("name", 64)
val sport = reference("sport", Sports, onDelete = ReferenceOption.SET_NULL).nullable()
val createdAt = datetime("created_at")
val updatedAt = datetime("updated_at")
}
Expand All @@ -22,6 +24,7 @@ class TeamTagEntity(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<TeamTagEntity>(TeamTags)

var name by TeamTags.name
var sport by SportEntity optionalReferencedOn TeamTags.sport
var createdAt by TeamTags.createdAt
var updatedAt by TeamTags.updatedAt
val teams by TeamEntity optionalReferrersOn Teams.teamTag
Expand All @@ -30,6 +33,7 @@ class TeamTagEntity(id: EntityID<Int>) : IntEntity(id) {
return TeamTag(
id.value,
name,
sport?.id?.value,
createdAt.toString(),
updatedAt.toString(),
)
Expand All @@ -40,11 +44,13 @@ class TeamTagEntity(id: EntityID<Int>) : IntEntity(id) {
data class TeamTag(
val id: Int,
val name: String,
val sportId: Int?,
val createdAt: String,
val updatedAt: String,
)

@Serializable
data class OmittedTeamTag(
val name: String,
val sportId: Int?,
)
10 changes: 10 additions & 0 deletions src/main/kotlin/net/sportsday/services/TeamTagsService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.sportsday.services

import io.ktor.server.plugins.*
import net.sportsday.models.OmittedTeamTag
import net.sportsday.models.SportEntity
import net.sportsday.models.TeamTag
import net.sportsday.models.TeamTagEntity
import org.jetbrains.exposed.sql.transactions.transaction
Expand Down Expand Up @@ -42,8 +43,13 @@ object TeamTagsService {

fun create(omittedTeamTag: OmittedTeamTag): Result<TeamTag> {
val model = transaction {
val sport = omittedTeamTag.sportId?.let {
SportEntity.findById(it) ?: throw NotFoundException("invalid sport id")
}

val teamTag = TeamTagEntity.new {
this.name = omittedTeamTag.name
this.sport = sport
this.createdAt = LocalDateTime.now()
this.updatedAt = LocalDateTime.now()
}
Expand All @@ -57,8 +63,12 @@ object TeamTagsService {
fun edit(id: Int, omittedTeamTag: OmittedTeamTag): Result<TeamTag> {
val model = transaction {
val teamTag = TeamTagEntity.findById(id) ?: throw NotFoundException("TeamTag not found.")
val sport = omittedTeamTag.sportId?.let {
SportEntity.findById(it) ?: throw NotFoundException("invalid sport id")
}

teamTag.name = omittedTeamTag.name
teamTag.sport = sport
teamTag.updatedAt = LocalDateTime.now()

teamTag.serializableModel()
Expand Down

0 comments on commit d22b9e2

Please sign in to comment.