Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

未完成・途中 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions memo
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"fmt"
"os"
"io"
"time"
"database/sql"
"godotenv"
"github.com/labstack/echo"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)



//task
//問題を管理するデータベースを作る
//追加・更新・取得APIを作る
//Code Tips Language
//ワンライナー DBに入れてデータを取得
//DBで登録しているものを渡す・問題の受け渡し



type Question struct {
gorm.Model
Id string
Code string
Tips string
Language string
}


func (u Question) String() string {
return fmt.Sprintf("Id:%s \n Code:%s \n Tips:%d \n Language:%d \n",
u.Id,
u.Code,
u.Tips,
u.Language,
)
}

// ユーザーを登録
func BaseAPI_user() echo.HandlerFunc {
return func(c echo.Context) error {
db := databases.Connect()
defer db.Close()

//リクエストボディ
user := new(Question)

user1 := Question{
Id: user.Id,
Code: user.Code,
Tips: user.Tips,
Language: user.Language,
}

//追加
insertUsers := []Question{user1}
insert(insertUsers, db)

//更新
update(user1, db)

//取得


return c.JSON()
}
}

func insert(users []Question, db *gorm.DB) {
for _, user := range users {
db.NewRecord(user)
db.Create(&user)
}
}

func update(users Question, db *gorm.DB) {
var user Question
db.Model(&user).Where("id = ?", 1).Update(map[string]interface{}{"Id": users.Id, "Code": users.Code, "Tips": users.Tips, "Language": users.Language})
}



func search(age int, db *gorm.DB) ([]Question) {
var user []Question
db.Raw("SELECT * FROM users WHERE code = ? ", age).Scan(&user)

return user
}


// SQLConnect DB接続

func GormConnect() (database *gorm.DB) {
// パスワード等を.envファイルから読み取る
// program > go > .env
err := godotenv.Load(fmt.Sprintf("../%s.env", os.Getenv("GO_ENV")))
if err != nil {
panic(err.Error())
} else {
fmt.Println("ok")
}

DBMS := "mysql" // MySQL
PROTOCOL := "tcp(localhost:3306)" // db:3306
DBNAME := "" // テーブル名
USER := "" // MySQLユーザー名
PASS := "" // パスワード

CONNECT := USER + ":" + PASS + "@" + PROTOCOL + "/" + DBNAME + "?charset=utf8&parseTime=true&loc=Asia%2FTokyo"
db, err := gorm.Open(DBMS, CONNECT)
if err != nil {
panic(err.Error())
} else {
fmt.Println("DB接続成功")
}
return db
}
6 changes: 6 additions & 0 deletions server/database/mysql/1_create_room.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE `rooms` (
`room_id` varchar(255) COLLATE utf8mb4_bin NOT NULL,
`room_name` varchar(255) COLLATE utf8mb4_bin NOT NULL,
`max_count` int COLLATE utf8mb4_bin NOT NULL,
PRIMARY KEY (room_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
3 changes: 3 additions & 0 deletions server/database/mysql/2_insert_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSERT INTO rooms (`room_id`, `room_name`,`max_count`) VALUES ("c1", "n1", 1);
INSERT INTO rooms (`room_id`, `room_name`,`max_count`) VALUES ("c2", "n2", 2);
INSERT INTO rooms (`room_id`, `room_name`,`max_count`) VALUES ("c3", "n3", 3);
7 changes: 7 additions & 0 deletions server/database/mysql/3_create_member_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE `members` (
`user_id` varchar(255) COLLATE utf8mb4_bin NOT NULL,
`user_name` varchar(255) COLLATE utf8mb4_bin NOT NULL,
`room_id` varchar(255) COLLATE utf8mb4_bin NOT NULL,
FOREIGN KEY (room_id) REFERENCES rooms(room_id) ON DELETE CASCADE,
PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
3 changes: 3 additions & 0 deletions server/database/mysql/4_insert_member_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSERT INTO members (`user_id`,`user_name`,`room_id`) VALUES ("c1", "testname1", "c1");
INSERT INTO members (`user_id`,`user_name`,`room_id`) VALUES ("c2", "testname2", "c2");
INSERT INTO members (`user_id`,`user_name`,`room_id`) VALUES ("c3", "testname3", "c3");
32 changes: 32 additions & 0 deletions server/src/config/dsn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package config

import (
"fmt"
"os"
)

// DSNはdataSourceNameを返します、もし必須の環境変数が設定されてなかった場合はerrorを返す
func DSN() (string, error) {
if os.Getenv("ENVIROMENT") == "PRODUCTION" {
return os.Getenv("POSTGRES_URL"), nil
}

// DBの環境変数
dbHost := os.Getenv("POSTGRES_HOST")
dbPort := os.Getenv("POSTGRES_PORT")
dbUser := os.Getenv("POSTGRES_USER")
dbPassword := os.Getenv("POSTGRES_PASSWORD")
dbName := os.Getenv("POSTGRES_DB")

if dbUser == "" || dbPassword == "" || dbHost == "" || dbPort == "" || dbName == "" {
return "", fmt.Errorf("ERROR : required environment variable not found")
}
return fmt.Sprintf(
"user=%s password=%s host=%s port=%s dbname=%s sslmode=disable",
dbUser,
dbPassword,
dbHost,
dbPort,
dbName,
), nil
}
7 changes: 7 additions & 0 deletions server/src/config/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package config

import "github.com/Doer-org/hack-camp_vol9_2022-2/utils"

func Port() string {
return utils.GetEnvOrDefault("PORT", "8080")
}
9 changes: 9 additions & 0 deletions server/src/domain/entity/member.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package entity

type Member struct {
UserId string
UserName string
RoomId string
}

type Members []*Member
8 changes: 8 additions & 0 deletions server/src/domain/entity/question.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package entity

type Question struct {
Id string
Code string
Tips string
Language string
}
7 changes: 7 additions & 0 deletions server/src/domain/entity/room.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package entity

type Room struct {
RoomId string
RoomName string
MaxCount int
}
9 changes: 9 additions & 0 deletions server/src/domain/repository/member.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package repository

import "github.com/Doer-org/hack-camp_vol9_2022-2/domain/entity"

type IMemberRepository interface {
CreateMember(userId string, userName string, roomId string) (*entity.Member, error)
GetAllMembersOfRoomID(roomId string) (entity.Members, error)
DeleteAllMembersOfRoomID(roomId string) error
}
10 changes: 10 additions & 0 deletions server/src/domain/repository/quesiton.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package repository

import "github.com/Doer-org/hack-camp_vol9_2022-2/domain/entity"

type QuestionRepository interface {
CreateQuestion(id string, code string, tips int, language int) (*entity.Question, error)
GetQuestionOfID(id string) (*entity.Question, error)
DeleteAllQuestion() error
DeleteQuestionOfID(id string) error
}
10 changes: 10 additions & 0 deletions server/src/domain/repository/room.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package repository

import "github.com/Doer-org/hack-camp_vol9_2022-2/domain/entity"

type IRoomRepository interface {
NewRoom(id string, name string, max_member int, member_count int) (*entity.Room, error)
GetRoomOfID(id string) (*entity.Room, error)
DeleteAllRoom() error
DeleteRoomOfID(id string) error
}
Binary file added server/src/error/.DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions server/src/error/infrastructure/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package infrastructure

import "errors"

var (

//roomのエラー
StatementError = errors.New("statement error")
ExecError = errors.New("exec error")
QueryrowError = errors.New("queryrow error")
RowsScanError = errors.New("Rows Scan error")
)
1 change: 1 addition & 0 deletions server/src/error/presentation/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package presentation
19 changes: 19 additions & 0 deletions server/src/error/usecase/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package usecase

import "errors"

var (

//roomのエラー
IdEmptyError = errors.New("room id empty")
NameEmptyError = errors.New("name empty")
MaxCountError = errors.New("max count failed")
RoomdIdUsedError = errors.New("room id already used")
RoomdIdEmptyError = errors.New("room id empty")

//chatのエラー
MessageEmptyError = errors.New("message empty")
SizeEmptyError = errors.New("size empty")
RoomIdEmptyError = errors.New("room id empty")
MemberIdEmptyError = errors.New("room id already used")
)
Binary file added server/src/infrastructure/.DS_Store
Binary file not shown.
33 changes: 33 additions & 0 deletions server/src/infrastructure/databese/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package database

import (
"database/sql"
"fmt"
"log"

_ "github.com/go-sql-driver/mysql"

"github.com/Doer-org/hack-camp_vol9_2022-2/config"
)

func NewDB() (*sql.DB, error) {
dbDSN, err := config.DSN()
if err != nil {
return nil, err
}
db, err := sql.Open("mysql", dbDSN)
if err != nil {
return nil, fmt.Errorf("failed to open MySQL : %w", err)
}

err = db.Ping()

if err != nil {
log.Println("DB接続失敗")
panic(err)
} else {
log.Println("DB接続成功")
}

return db, err
}
Loading