Commit b793e986 authored by jadtaphon chinnapan's avatar jadtaphon chinnapan

jadtaphon

parent e4aa5658
Pipeline #904 canceled with stages
package main
import (
"net/http"
"github.com/labstack/echo"
"gopkg.in/mgo.v2/bson"
)
//////////sssss///////////////////////////////////////
func (h *Handler) checkIn(c echo.Context) error {
u := new(Course)
id := c.Param("id")
stdid := c.FormValue("stdid")
week := c.FormValue("week")
//idactive := c.FormValue("id_active")
// Find user
db := h.DB.Clone()
defer db.Close()
query := bson.M{"_id": bson.ObjectIdHex(id), "students.id_student": stdid}
update := bson.M{}
switch week {
case "1":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week1.week": 1,
"students.$.weeks.week1.id_active": stdid,
},
}
case "2":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week2.week": 1,
"students.$.weeks.week2.id_active": stdid,
},
}
case "3":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week3.week": 1,
"students.$.weeks.week3.id_active": stdid,
},
}
case "4":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week4.week": 1,
"students.$.weeks.week4.id_active": stdid,
},
}
case "5":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week5.week": 1,
"students.$.weeks.week5.id_active": stdid,
},
}
case "6":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week6.week": 1,
"students.$.weeks.week6.id_active": stdid,
},
}
case "7":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week7.week": 1,
"students.$.weeks.week7.id_active": stdid,
},
}
case "8":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week8.week": 1,
"students.$.weeks.week8.id_active": stdid,
},
}
case "9":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week9.week": 1,
"students.$.weeks.week9.id_active": stdid,
},
}
case "10":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week10.week": 1,
"students.$.weeks.week10.id_active": stdid,
},
}
case "11":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week11.week": 1,
"students.$.weeks.week11.id_active": stdid,
},
}
case "12":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week12.week": 1,
"students.$.weeks.week12.id_active": stdid,
},
}
case "13":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week13.week": 1,
"students.$.weeks.week13.id_active": stdid,
},
}
case "14":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week14.week": 1,
"students.$.weeks.week14.id_active": stdid,
},
}
case "15":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week15.week": 1,
"students.$.weeks.week15.id_active": stdid,
},
}
case "16":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week16.week": 1,
"students.$.weeks.week16.id_active": stdid,
},
}
default:
return c.JSON(http.StatusBadRequest, "week invalid")
}
err := db.DB("test").C("course").Update(query, update)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
WsSendAction(c, WsMsg{
Action: string(checknameEvent),
Payload: map[string]interface{}{"item": u},
})
return c.JSON(http.StatusOK, u)
}
func (h *Handler) checknameT(c echo.Context) error {
u := new(Course)
id := c.Param("id")
stdid := c.FormValue("stdid")
week := c.FormValue("week")
//cont := c.FormValue("cont")
// Find user
db := h.DB.Clone()
defer db.Close()
query := bson.M{"_id": bson.ObjectIdHex(id), "students.id_student": stdid}
update := bson.M{}
switch week {
case "1":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week1.week": 0.5,
"students.$.weeks.week1.id_active": stdid,
},
}
case "2":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week2.week": 0.5,
"students.$.weeks.week2.id_active": stdid,
},
}
case "3":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week3.week": 0.5,
"students.$.weeks.week3.id_active": stdid,
},
}
case "4":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week4.week": 0.5,
"students.$.weeks.week4.id_active": stdid,
},
}
case "5":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week5.week": 0.5,
"students.$.weeks.week5.id_active": stdid,
},
}
case "6":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week6.week": 0.5,
"students.$.weeks.week6.id_active": stdid,
},
}
case "7":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week7.week": 0.5,
"students.$.weeks.week7.id_active": stdid,
},
}
case "8":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week8.week": 0.5,
"students.$.weeks.week8.id_active": stdid,
},
}
case "9":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week9.week": 0.5,
"students.$.weeks.week9.id_active": stdid,
},
}
case "10":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week10.week": 0.5,
"students.$.weeks.week10.id_active": stdid,
},
}
case "11":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week11.week": 0.5,
"students.$.weeks.week11.id_active": stdid,
},
}
case "12":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week12.week": 0.5,
"students.$.weeks.week12.id_active": stdid,
},
}
case "13":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week13.week": 0.5,
"students.$.weeks.week13.id_active": stdid,
},
}
case "14":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week14.week": 0.5,
"students.$.weeks.week14.id_active": stdid,
},
}
case "15":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week15.week": 0.5,
"students.$.weeks.week15.id_active": stdid,
},
}
case "16":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week16.week": 0.5,
"students.$.weeks.week16.id_active": stdid,
},
}
default:
return c.JSON(http.StatusBadRequest, "week invalid")
}
err := db.DB("test").C("course").Update(query, update)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, u)
}
func (h *Handler) checknameO(c echo.Context) error {
u := new(Course)
id := c.Param("id")
stdid := c.FormValue("stdid")
week := c.FormValue("week")
//cont := c.FormValue("cont")
// Find user
db := h.DB.Clone()
defer db.Close()
query := bson.M{"_id": bson.ObjectIdHex(id), "students.id_student": stdid}
update := bson.M{}
switch week {
case "1":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week1.week": 0,
"students.$.weeks.week1.id_active": stdid,
},
}
case "2":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week2.week": 0,
"students.$.weeks.week2.id_active": stdid,
},
}
case "3":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week3.week": 0,
"students.$.weeks.week3.id_active": stdid,
},
}
case "4":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week4.week": 0,
"students.$.weeks.week4.id_active": stdid,
},
}
case "5":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week5.week": 0,
"students.$.weeks.week5.id_active": stdid,
},
}
case "6":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week6.week": 0,
"students.$.weeks.week6.id_active": stdid,
},
}
case "7":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week7.week": 0,
"students.$.weeks.week7.id_active": stdid,
},
}
case "8":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week8.week": 0,
"students.$.weeks.week8.id_active": stdid,
},
}
case "9":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week9.week": 0,
"students.$.weeks.week9.id_active": stdid,
},
}
case "10":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week10.week": 0,
"students.$.weeks.week10.id_active": stdid,
},
}
case "11":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week11.week": 0,
"students.$.weeks.week11.id_active": stdid,
},
}
case "12":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week12.week": 0,
"students.$.weeks.week12.id_active": stdid,
},
}
case "13":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week13.week": 0,
"students.$.weeks.week13.id_active": stdid,
},
}
case "14":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week14.week": 0,
"students.$.weeks.week14.id_active": stdid,
},
}
case "15":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week15.week": 0,
"students.$.weeks.week15.id_active": stdid,
},
}
case "16":
update = bson.M{
"$set": bson.M{
"students.$.weeks.week16.week": 0,
"students.$.weeks.week16.id_active": stdid,
},
}
default:
return c.JSON(http.StatusBadRequest, "week invalid")
}
err := db.DB("test").C("course").Update(query, update)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, u)
}
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
// Maximum message size allowed from peer.
maxMessageSize = 512
)
var (
newline = []byte{'\n'}
space = []byte{' '}
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
// Client is a middleman between the websocket connection and the hub.
type Client struct {
hub *Hub
// The websocket connection.
conn *websocket.Conn
// Buffered channel of outbound messages.
send chan []byte
}
// readPump pumps messages from the websocket connection to the hub.
//
// The application runs readPump in a per-connection goroutine. The application
// ensures that there is at most one reader on a connection by executing all
// reads from this goroutine.
func (c *Client) readPump() {
defer func() {
c.hub.unregister <- c
c.conn.Close()
}()
c.conn.SetReadLimit(maxMessageSize)
c.conn.SetReadDeadline(time.Now().Add(pongWait))
c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, message, err := c.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
}
break
}
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
c.hub.broadcast <- message
}
}
// writePump pumps messages from the hub to the websocket connection.
//
// A goroutine running writePump is started for each connection. The
// application ensures that there is at most one writer to a connection by
// executing all writes from this goroutine.
func (c *Client) writePump() {
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
c.conn.Close()
}()
for {
select {
case message, ok := <-c.send:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if !ok {
// The hub closed the channel.
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
w, err := c.conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
w.Write(message)
// Add queued chat messages to the current websocket message.
n := len(c.send)
for i := 0; i < n; i++ {
w.Write(newline)
w.Write(<-c.send)
}
if err := w.Close(); err != nil {
return
}
case <-ticker.C:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return
}
}
}
}
// serveWs handles websocket requests from the peer.
func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
client.hub.register <- client
// Allow collection of memory referenced by the caller by doing all work in
// new goroutines.
go client.writePump()
go client.readPump()
}
module server
go 1.13
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gorilla/websocket v1.4.1
github.com/kr/pretty v0.1.0 // indirect
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.3.0 // indirect
github.com/valyala/fasttemplate v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
gopkg.in/yaml.v2 v2.2.7 // indirect
)
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy1Ocw4=
github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876 h1:sKJQZMuxjOAR/Uo2LBfU90onWEf1dF4C+0hPJCc9Mpc=
golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
package main
import (
"gopkg.in/mgo.v2"
)
type (
Handler struct {
DB *mgo.Session
}
)
const (
Key = "secret"
)
package main
// Msg type
type WsMsg struct {
Action string `json:"action"`
Payload map[string]interface{} `json:"payload"`
}
// Hub maintains the set of active clients and broadcasts messages to the
// clients.
type Hub struct {
// Registered clients.
clients map[*Client]bool
// Inbound messages from the clients.
broadcast chan []byte
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
event chan WsMsg
}
type Event string
const (