Skip to content

Commit

Permalink
✨ feat: Implement new models and migration logic
Browse files Browse the repository at this point in the history
- Implemented various model classes in /models, including User, Class, ClassUser, GroupBoard, GroupCode, GroupSchedule, Role, and Attendance.
- Each model class is designed with appropriate GORM tags for effective database mapping.
- Updated the migration logic in /migration/migration.go to include these new models.
- Modified main.go to facilitate optional migration execution based on the RUN_MIGRATIONS environment variable.
- Removed unnecessary logic for migration.

Related issue: #15
  • Loading branch information
yuminn-k committed Feb 21, 2024
1 parent 3177059 commit 34cd042
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
ENV=prod
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_DATABASE=
MYSQL_HOST=
MYSQL_PORT=
RUN_MIGRATIONS=
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
docs "github.com/YJU-OKURA/project_minori-gin-deployment-repo/docs"
"github.com/YJU-OKURA/project_minori-gin-deployment-repo/migration"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"net/http"
"os"
)

// @BasePath /api/v1
Expand All @@ -24,7 +26,16 @@ func Helloworld(g *gin.Context) {
}

func main() {
//infra.Initialize()
// DBの初期化
db := migration.InitDB()

// RUN_MIGRATIONSがtrueの場合、マイグレーションを実行
if os.Getenv("RUN_MIGRATIONS") == "true" {
migration.Migrate(db)
os.Exit(0)
}

// Ginの初期化
r := gin.Default()
docs.SwaggerInfo.BasePath = "/api/v1"
v1 := r.Group("/api/v1")
Expand Down
1 change: 0 additions & 1 deletion migration/main.go

This file was deleted.

41 changes: 41 additions & 0 deletions migration/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package migration

import (
"fmt"
"github.com/YJU-OKURA/project_minori-gin-deployment-repo/models"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"os"
)

func InitDB() *gorm.DB {
user := os.Getenv("MYSQL_USER")
pass := os.Getenv("MYSQL_PASSWORD")
dbName := os.Getenv("MYSQL_DATABASE")
host := os.Getenv("MYSQL_HOST")
port := os.Getenv("MYSQL_PORT")

dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, pass, host, port, dbName)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
return db
}

func Migrate(db *gorm.DB) {
err := db.AutoMigrate(
&models.User{},
&models.Class{},
&models.ClassUser{},
&models.GroupBoard{},
&models.GroupCode{},
&models.GroupSchedule{},
&models.Role{},
&models.Attendance{},
)
if err != nil {
log.Fatalf("failed to migrate database: %v", err)
}
}
1 change: 0 additions & 1 deletion migration/migrations/1_users.down.sql

This file was deleted.

Empty file.
10 changes: 10 additions & 0 deletions models/attendance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type Attendance struct {
Key string `gorm:"primaryKey;size:255"`
CID uint `gorm:"not null"` // Class ID
UID uint `gorm:"not null"` // User ID
AttendanceID string `gorm:"size:255;not null"`
IsAttendance string `gorm:"type:enum('Attendance', 'Tardy', 'Absence');default:'Absence';not null"` // 出席, 遅刻, 欠席
ClassUser ClassUser `gorm:"foreignKey:CID,UID"`
}
9 changes: 9 additions & 0 deletions models/class.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package models

type Class struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"size:30;not null"`
Limitation *int
Description *string `gorm:"size:255"`
Image *string `gorm:"size:255"`
}
11 changes: 11 additions & 0 deletions models/class_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

type ClassUser struct {
CID uint `gorm:"primaryKey"`
UID uint `gorm:"primaryKey"`
Nickname string `gorm:"size:50;not null"`
IsFavorite bool
RoleID int `gorm:"not null"`
Class Class `gorm:"foreignKey:CID"`
User User `gorm:"foreignKey:UID"`
}
17 changes: 17 additions & 0 deletions models/group_board.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package models

import "time"

type GroupBoard struct {
ID uint `gorm:"primaryKey"`
Title string `gorm:"size:255;not null"`
Content string `gorm:"type:text;not null"`
Image string `gorm:"size:255"`
CreatedAt time.Time `gorm:"not null;"`
UpdatedAt time.Time `gorm:"not null;"`
IsAnnounced bool `gorm:"not null;default:false"`
CID uint `gorm:"not null"` // Class ID
UID uint `gorm:"not null"` // User ID
Class Class `gorm:"foreignKey:CID"`
User User `gorm:"foreignKey:UID"`
}
11 changes: 11 additions & 0 deletions models/group_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

type GroupCode struct {
ID string `gorm:"primaryKey;size:255"`
Code string `gorm:"size:10;not null"`
Secret string `gorm:"size:20"`
CID uint `gorm:"not null"` // Class ID
UID uint `gorm:"not null"` // User ID
Class Class `gorm:"foreignKey:CID"`
User User `gorm:"foreignKey:UID"`
}
13 changes: 13 additions & 0 deletions models/group_schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package models

import "time"

type GroupSchedule struct {
ID uint `gorm:"primaryKey"`
Title string `gorm:"size:255;not null"`
StartedAt time.Time `gorm:"not null"`
EndedAt time.Time `gorm:"not null"`
CID uint `gorm:"not null"` // Class ID
IsLive bool `gorm:"not null;default:false"`
Class Class `gorm:"foreignKey:CID"`
}
6 changes: 6 additions & 0 deletions models/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models

type Role struct {
ID int `gorm:"primaryKey"`
Role string `gorm:"type:enum('USER', 'ADMIN', 'ASSISTANT', 'APPLICANT', 'BLACKLIST');not null"`
}
12 changes: 12 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
package models

import (
"time"
)

type User struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"size:50;not null"`
Email string `gorm:"size:100;not null;"`
Image string `gorm:"size:255;not null;"`
CreatedAt time.Time `gorm:"not null;"`
}

0 comments on commit 34cd042

Please sign in to comment.