Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
fuji8 committed Aug 6, 2020
2 parents 1132b64 + e89e6ae commit 345b27d
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 34 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM node:13.12.0-alpine as web-build
FROM node:14.5.0-alpine as web-build

WORKDIR /github.com/traPtitech/knoq/web

COPY ./web ./
RUN yarn
RUN yarn build

FROM golang:1.13.8-alpine as server-build
FROM golang:1.14.6-alpine as server-build

WORKDIR /github.com/traPtitech/knoq

Expand All @@ -17,7 +17,7 @@ COPY ./ ./

RUN go build -o knoq

FROM alpine:3.9
FROM alpine:3.12.0

WORKDIR /app

Expand Down
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
module room

go 1.13
go 1.14

require (
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82
github.com/go-sql-driver/mysql v1.5.0
github.com/gofrs/uuid v3.2.0+incompatible
github.com/gofrs/uuid v3.3.0+incompatible
github.com/gorilla/sessions v1.2.0
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
github.com/jinzhu/gorm v1.9.12
github.com/json-iterator/go v1.1.9
github.com/jinzhu/gorm v1.9.15
github.com/json-iterator/go v1.1.10
github.com/labstack/echo-contrib v0.8.1-0.20200115200653-2d4a7f3c41d8
github.com/labstack/echo/v4 v4.1.16
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.1
github.com/traPtitech/traQ v1.0.0-rc.2.0.20200309071637-82e15593b8c0
github.com/wader/gormstore v0.0.0-20190904144442-d36772af4310
go.uber.org/zap v1.15.0
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
google.golang.org/api v0.22.0
google.golang.org/api v0.29.0
gopkg.in/gormigrate.v1 v1.6.0
)
55 changes: 40 additions & 15 deletions go.sum

Large diffs are not rendered by default.

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

import (
"gopkg.in/gormigrate.v1"
)

// Migrations is all db migrations
func Migrations() []*gormigrate.Migration {
return []*gormigrate.Migration{
v1(),
}
}
31 changes: 31 additions & 0 deletions migration/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package migration

import (
"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

// Migrate execute migrations
func Migrate(db *gorm.DB, tables []interface{}) error {
m := gormigrate.New(db, gormigrate.DefaultOptions, Migrations())

m.InitSchema(func(tx *gorm.DB) error {
mv1 := gormigrate.New(tx, gormigrate.DefaultOptions, []*gormigrate.Migration{
{
ID: "assume existing DB",
Migrate: v1().Migrate,
},
})
err := mv1.Migrate()
if err != nil {
return err
}
err = tx.AutoMigrate(tables...).Error
if err != nil {
return err
}

return nil
})
return m.Migrate()
}
19 changes: 19 additions & 0 deletions migration/v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Package migration migrate current struct
package migration

import (
"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

// v1 unique_index:idx_room_uniqueの削除
func v1() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "1",
Migrate: func(db *gorm.DB) error {
return db.
Table("rooms").
RemoveIndex("idx_room_unique").Error
},
}
}
8 changes: 4 additions & 4 deletions repository/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ type GroupUsers struct {
// Room 部屋情報
type Room struct {
ID uuid.UUID `json:"id" gorm:"type:char(36);primary_key"`
Place string `json:"place" gorm:"type:varchar(32);unique_index:idx_room_unique"`
Public bool `gorm:"unique_index:idx_room_unique"`
TimeStart time.Time `json:"timeStart" gorm:"type:DATETIME; unique_index:idx_room_unique; index"`
TimeEnd time.Time `json:"timeEnd" gorm:"type:DATETIME; unique_index:idx_room_unique; index"`
Place string `json:"place" gorm:"type:varchar(32);"`
Public bool
TimeStart time.Time `json:"timeStart" gorm:"type:DATETIME; index"`
TimeEnd time.Time `json:"timeEnd" gorm:"type:DATETIME; index"`
Events []Event `gorm:"foreignkey:RoomID"`
CreatedBy uuid.UUID `gorm:"type:char(36)"`
Model
Expand Down
7 changes: 4 additions & 3 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"os"

"room/migration"

"github.com/jinzhu/gorm"
"go.uber.org/zap"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -186,9 +188,8 @@ func SetupDatabase() (*gorm.DB, error) {
func initDB(db *gorm.DB) error {
// gormのエラーの上書き
gorm.ErrRecordNotFound = ErrNotFound

// テーブルが無ければ作成
if err := db.AutoMigrate(tables...).Error; err != nil {
// db.LogMode(true)
if err := migration.Migrate(db, tables); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion repository/room_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestGormRepository_CreateRoom(t *testing.T) {

t.Run("duplicate room", func(t *testing.T) {
if room2, err := repo.CreateRoom(params); assert.NoError(t, err) {
assert.Equal(t, room.ID, room2.ID)
assert.NotEqual(t, room.ID, room2.ID)
}
})
}
Expand Down
26 changes: 25 additions & 1 deletion router/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func (h *Handlers) HandleSetRooms(c echo.Context) error {
return judgeErrorResponse(err)
}
res := make([]*service.RoomRes, 0)
for _, room := range googleRooms {
currentRooms, err := h.Repo.GetAllRooms(&now, nil)
if err != nil {
return judgeErrorResponse(err)
}
filterdRooms := filterSameRooms(currentRooms, googleRooms)
for _, room := range filterdRooms {
roomParams := new(repo.WriteRoomParams)
err := copier.Copy(&roomParams, room)
if err != nil {
Expand Down Expand Up @@ -144,3 +149,22 @@ func (h *Handlers) HandleDeletePrivateRoom(c echo.Context) error {
func setCreatedBytoRoom(c echo.Context, roomParams *repo.WriteRoomParams) {
roomParams.CreatedBy, _ = getRequestUserID(c)
}

// filterSameRooms currentRoomsにあるroomと
// 同一なroom(Place, Public ,TimeStart, TimeEndが同一)を
// targetRoomsから削除したものを返します。
func filterSameRooms(currentRooms []*repo.Room, targetRooms []*repo.Room) []*repo.Room {
rooms := make([]*repo.Room, 0)
for _, t := range targetRooms {
alreadyExist := false
for _, c := range currentRooms {
if t.Place == c.Place && t.Public == c.Public && t.TimeStart.Equal(c.TimeStart) && t.TimeEnd.Equal(c.TimeEnd) {
alreadyExist = true
}
}
if !alreadyExist {
rooms = append(rooms, t)
}
}
return rooms
}
67 changes: 67 additions & 0 deletions router/rooms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package router

import (
"reflect"
repo "room/repository"
"testing"
"time"
)

func Test_filterSameRooms(t *testing.T) {
type args struct {
currentRooms []*repo.Room
targetRooms []*repo.Room
}
baseRoom := repo.Room{
Place: "sample place",
Public: true,
TimeStart: time.Now(),
TimeEnd: time.Now().Add(3 * time.Hour),
}
room1 := baseRoom
room1.Place = "room1"
room2 := baseRoom
room2.Place = "room2"
room3 := baseRoom
room3.Place = "room3"

tests := []struct {
name string
args args
want []*repo.Room
}{
{
name: "nil",
args: args{
currentRooms: []*repo.Room{
&baseRoom,
},
targetRooms: []*repo.Room{
&baseRoom,
},
},
want: []*repo.Room{},
},
{
name: "one ",
args: args{
currentRooms: []*repo.Room{
&room1, &room2,
},
targetRooms: []*repo.Room{
&room1, &room2, &room3,
},
},
want: []*repo.Room{
&room3,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := filterSameRooms(tt.args.currentRooms, tt.args.targetRooms); !reflect.DeepEqual(got, tt.want) {
t.Errorf("filterSameRooms() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion web
Submodule web updated from 92efb8 to 2c13f2

0 comments on commit 345b27d

Please sign in to comment.