Skip to content

Commit

Permalink
Merge pull request #209 from traPtitech/fix/issue194
Browse files Browse the repository at this point in the history
Fix/issue194
  • Loading branch information
fuji8 committed Sep 23, 2021
2 parents 652f717 + 941392f commit f95cc79
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ paths:
End time: *csvTime
Location:
type: string
example: "Subject, Start date, End date, Start time, End time, Location\n, 2006/01/02, 2006/01/02, 15:04, 15:04, S516\n"
responses:
'201':
$ref: '#/components/responses/RoomArray'
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.1
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
github.com/jszwec/csvutil v1.5.1 // indirect
github.com/labstack/echo-contrib v0.8.1-0.20200115200653-2d4a7f3c41d8
github.com/labstack/echo/v4 v4.2.0
github.com/lestrrat-go/bufferpool v0.0.0-20180220091733-e7784e1b3e37 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jszwec/csvutil v1.5.1 h1:c3GFBhj6DFMUl4dMK3+B6rz2+LWWS/e9VJiVJ9t9kfQ=
github.com/jszwec/csvutil v1.5.1/go.mod h1:Rpu7Uu9giO9subDyMCIQfHVDuLrcaC36UA4YcJjGBkg=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
Expand Down
1 change: 1 addition & 0 deletions presentation/converter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions presentation/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/traPtitech/knoQ/domain"
)

//go:generate gotypeconverter -s RoomReq -d domain.WriteRoomParams -o converter.go .
Expand All @@ -14,6 +15,15 @@ type RoomReq struct {
Admins []uuid.UUID `json:"admins"`
}

type RoomCSVReq struct {
Subject string `csv:"Subject"`
StartDate string `csv:"Start date"`
EndDate string `csv:"End date"`
StartTime string `csv:"Start time"`
EndTime string `csv:"End time"`
Location string `csv:"Location"`
}

type StartEndTime struct {
TimeStart time.Time `json:"timeStart"`
TimeEnd time.Time `json:"timeEnd"`
Expand All @@ -32,3 +42,27 @@ type RoomRes struct {
CreatedBy uuid.UUID `json:"createdBy"`
Model
}

func ChangeRoomCSVReqTodomainWriteRoomParams(src RoomCSVReq, userID uuid.UUID) (*domain.WriteRoomParams, error) {

layout := "2006/01/02 15:04"
jst, _ := time.LoadLocation("Asia/Tokyo")
var params domain.WriteRoomParams
var err error = nil

params.Place = src.Location
params.TimeStart, err = time.ParseInLocation(layout, src.StartDate+" "+src.StartTime, jst)
if err != nil {
return nil, err
}

params.TimeEnd, err = time.ParseInLocation(layout, src.EndDate+" "+src.EndTime, jst)
if err != nil {
return nil, err
}

params.Admins = []uuid.UUID{userID}

return &params, err

}
32 changes: 31 additions & 1 deletion router/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,37 @@ func (h *Handlers) HandlePostRoom(c echo.Context) error {

// HandleCreateVerifedRooms csvを解析し、進捗部屋を作成
func (h *Handlers) HandleCreateVerifedRooms(c echo.Context) error {
return c.NoContent(501)

userID, err := getRequestUserID(c)
if err != nil {
return notFound(err)
}

var req []presentation.RoomCSVReq
if err := c.Bind(&req); err != nil {
return badRequest(err)
}

//構造体の変換
var RoomsRes []presentation.RoomRes

for _, v := range req {

params, err := presentation.ChangeRoomCSVReqTodomainWriteRoomParams(v, userID)
if err != nil {
return badRequest(err)
}

room, err := h.Repo.CreateVerifiedRoom(*params, getConinfo(c))

if err != nil {
return judgeErrorResponse(err)
}

RoomsRes = append(RoomsRes, presentation.ConvdomainRoomToRoomRes(*room))

}
return c.JSON(http.StatusCreated, RoomsRes)
}

// HandleGetRoom get one room
Expand Down
31 changes: 31 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
package router

import (
"bytes"
"io"
"net/http"
"strings"

"github.com/gofrs/uuid"
"github.com/jszwec/csvutil"
"github.com/traPtitech/knoQ/domain"

"github.com/gorilla/securecookie"
Expand All @@ -32,6 +35,7 @@ func (h *Handlers) SetupRoute() *echo.Echo {
echo.NotFoundHandler = NotFoundHandler
// echo初期化
e := echo.New()
e.Binder = &CustomBinder{}
e.HTTPErrorHandler = HTTPErrorHandler
e.Use(middleware.Recover())
e.Use(middleware.Secure())
Expand Down Expand Up @@ -156,3 +160,30 @@ func getConinfo(c echo.Context) *domain.ConInfo {
info.ReqUserID = uuid.FromStringOrNil(str)
return info
}

type CustomBinder struct{}

func (cb *CustomBinder) Bind(i interface{}, c echo.Context) error {
// You may use default binder
db := new(echo.DefaultBinder)
if err := db.Bind(i, c); err != echo.ErrUnsupportedMediaType {
return err
}

// Define your custom implementation here
rq := c.Request()
ctype := rq.Header.Get(echo.HeaderContentType)

if strings.HasPrefix(ctype, "text/csv") {

buf := new(bytes.Buffer)
io.Copy(buf, c.Request().Body)
data := buf.Bytes()
if err := csvutil.Unmarshal(data, i); err != nil {
return err
}
return nil
}

return echo.ErrUnsupportedMediaType
}

0 comments on commit f95cc79

Please sign in to comment.