You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "net/http"
  6. "github.com/gin-contrib/cors"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func main() {
  10. db := initDB("db/storage.db")
  11. router := gin.Default()
  12. if gin.Mode() != "release" {
  13. config := cors.DefaultConfig()
  14. config.AllowOrigins = []string{"http://localhost:8999", "http://127.0.0.1:8999", "http://[::]:8999", "http://[::1]:8999"}
  15. config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE"}
  16. router.Use(cors.New(config))
  17. }
  18. router.StaticFile("/", "frontend/dist/index.html")
  19. router.Static("/static/", "frontend/dist/static/")
  20. router.GET("/api/tasks/:account/:course", getTasks(db))
  21. router.POST("/api/tasks/:account/:course", addTask(db))
  22. router.PUT("/api/tasks/:account/:course/:id", updateTask(db))
  23. router.DELETE("/api/tasks/:account/:course/:id", deleteTask(db))
  24. router.GET("/api/courses/:account", getCourses(db))
  25. router.GET("/api/pres/:account/:course", getPres(db))
  26. router.PUT("/api/pres/:account/:course", setPres(db))
  27. router.GET("/api/perc/:account/:course", getPerc(db))
  28. router.PUT("/api/perc/:account/:course", setPerc(db))
  29. router.DELETE("/api/tasks/:account/:course", deleteCourse(db))
  30. router.Group("/api", func(c *gin.Context) {
  31. c.String(404, "API endpoint not found.\n")
  32. })
  33. router.Use(rewrite(router, `^/[a-zA-Z0-9-]+(?:/[^/]+)?$`, "/"))
  34. router.Run(":8900")
  35. }
  36. func deleteCourse(db *sql.DB) gin.HandlerFunc {
  37. return func(c *gin.Context) {
  38. resultL, err := statements["delLimits"].Exec(c.Param("account"), c.Param("course"))
  39. fmt.Printf("Deleted for user %s limits for course %s \n", c.Param("account"), c.Param("course"))
  40. assertNil(err)
  41. resultC, err := statements["delCourse"].Exec(c.Param("account"), c.Param("course"))
  42. fmt.Printf("Deleted for user %s course %s\n", c.Param("account"), c.Param("course"))
  43. assertNil(err)
  44. numC, _ := resultC.RowsAffected()
  45. numL, _ := resultL.RowsAffected()
  46. c.JSON(http.StatusOK, gin.H{
  47. "Number deleted Rows from Courses": numC,
  48. "Number deleted Rows from Limits": numL,
  49. "Deleted Course": c.Param("course"),
  50. "For User": c.Param("account"),
  51. })
  52. }
  53. }
  54. func getTasks(db *sql.DB) gin.HandlerFunc {
  55. return func(c *gin.Context) {
  56. result, err := statements["getTasks"].Query(c.Param("account"), c.Param("course"))
  57. fmt.Printf("%+v %s %s\n", result, c.Param("account"), c.Param("course"))
  58. assertNil(err)
  59. defer result.Close()
  60. resp := []GridLine{}
  61. for result.Next() {
  62. line := GridLine{}
  63. err = result.Scan(&line.Id, &line.Name, &line.Points, &line.MaxPoints, &line.Presentations)
  64. assertNil(err)
  65. resp = append(resp, line)
  66. }
  67. c.JSON(http.StatusOK, resp)
  68. }
  69. }
  70. func addTask(db *sql.DB) gin.HandlerFunc {
  71. return func(c *gin.Context) {
  72. task := GridLine{}
  73. err := c.BindJSON(&task)
  74. fmt.Printf("%+v %s %s\n", task, c.Param("account"), c.Param("course"))
  75. assertNil(err)
  76. result, err := statements["addTask"].Exec(c.Param("account"), c.Param("course"), task.Name, task.Points, task.MaxPoints, task.Presentations)
  77. assertNil(err)
  78. id, _ := result.LastInsertId()
  79. c.JSON(http.StatusOK, gin.H{
  80. "id": id,
  81. })
  82. }
  83. }
  84. func updateTask(db *sql.DB) gin.HandlerFunc {
  85. return func(c *gin.Context) {
  86. task := GridLine{}
  87. err := c.BindJSON(&task)
  88. assertNil(err)
  89. result, err := statements["updateTask"].Exec(task.Name, task.Points, task.MaxPoints, task.Presentations, c.Param("account"), c.Param("course"), c.Param("id"))
  90. assertNil(err)
  91. num, _ := result.RowsAffected()
  92. c.JSON(http.StatusOK, gin.H{
  93. "updated": num,
  94. })
  95. }
  96. }
  97. func deleteTask(db *sql.DB) gin.HandlerFunc {
  98. return func(c *gin.Context) {
  99. result, err := statements["deleteTask"].Exec(c.Param("account"), c.Param("course"), c.Param("id"))
  100. assertNil(err)
  101. num, _ := result.RowsAffected()
  102. c.JSON(http.StatusOK, gin.H{
  103. "deleted": num,
  104. })
  105. }
  106. }
  107. func getCourses(db *sql.DB) gin.HandlerFunc {
  108. return func(c *gin.Context) {
  109. result, err := statements["getCourses"].Query(c.Param("account"))
  110. assertNil(err)
  111. defer result.Close()
  112. resp := []UserLineArr{}
  113. for result.Next() {
  114. line := UserLine{}
  115. err = result.Scan(&line.Course, &line.Account, &line.Points, &line.MaxPoints, &line.Min, &line.MinType)
  116. assertNil(err)
  117. resultLine := UserLineArr{}
  118. resultLine.Course = line.Course
  119. if line.MinType.Valid && line.MinType.String == "percent" {
  120. if line.MaxPoints > 0 {
  121. resultLine.AccPerc = float64(100*line.Points) / float64(line.MaxPoints)
  122. } else {
  123. resultLine.AccPerc = 0
  124. }
  125. }
  126. if line.MinType.Valid && line.MinType.String == "points" {
  127. resultLine.AccPerc = float64(line.Points)
  128. }
  129. resultLine.Min = line.Min.Float64
  130. resultLine.MinType = line.MinType.String
  131. if resultLine.MinType == "" {
  132. resultLine.MinType = "none"
  133. }
  134. resp = append(resp, resultLine)
  135. }
  136. c.JSON(http.StatusOK, resp)
  137. }
  138. }
  139. func getPres(db *sql.DB) gin.HandlerFunc {
  140. return func(c *gin.Context) {
  141. result, err := statements["getPres"].Query(c.Param("course"), c.Param("account"))
  142. assertNil(err)
  143. defer result.Close()
  144. var resp int
  145. for result.Next() {
  146. line := presStruct{}
  147. err = result.Scan(&line.Pres)
  148. assertNil(err)
  149. if line.Pres.Valid {
  150. resp = int(line.Pres.Int64)
  151. }
  152. }
  153. c.JSON(http.StatusOK, gin.H{
  154. "presentations": resp,
  155. })
  156. }
  157. }
  158. func getPerc(db *sql.DB) gin.HandlerFunc {
  159. return func(c *gin.Context) {
  160. result, err := statements["getPerc"].Query(c.Param("course"), c.Param("account"))
  161. assertNil(err)
  162. defer result.Close()
  163. line := percStruct{}
  164. for result.Next() {
  165. err = result.Scan(&line.Minimum, &line.Type)
  166. assertNil(err)
  167. }
  168. c.JSON(http.StatusOK, line)
  169. }
  170. }
  171. func setPres(db *sql.DB) gin.HandlerFunc {
  172. return func(c *gin.Context) {
  173. pres := presSetStruct{}
  174. err := c.BindJSON(&pres)
  175. assertNil(err)
  176. result, err := statements["setPres"].Exec(c.Param("course"), c.Param("account"), c.Param("course"), c.Param("account"), c.Param("course"), c.Param("account"), c.Param("course"), c.Param("account"), pres.Pres)
  177. assertNil(err)
  178. id, _ := result.RowsAffected()
  179. c.JSON(http.StatusOK, gin.H{
  180. "updated presentations": id,
  181. })
  182. }
  183. }
  184. func setPerc(db *sql.DB) gin.HandlerFunc {
  185. return func(c *gin.Context) {
  186. perc := percStruct{}
  187. err := c.BindJSON(&perc)
  188. assertNil(err)
  189. result, err := statements["setPerc"].Exec(c.Param("course"), c.Param("account"), c.Param("course"), c.Param("account"), perc.Minimum, perc.Type, c.Param("course"), c.Param("account"))
  190. assertNil(err)
  191. id, _ := result.RowsAffected()
  192. c.JSON(http.StatusOK, gin.H{
  193. "updated percentage": id,
  194. })
  195. }
  196. }