mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-13 17:29:54 +01:00
Appointment feature
This commit is contained in:
101
ober.go
101
ober.go
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fasthttp/router"
|
||||
"github.com/valyala/fasthttp"
|
||||
@@ -26,9 +27,11 @@ func RunWebserv() {
|
||||
r.GET("/api/user/{user?}", Headers(UserGet))
|
||||
r.GET("/api/watch/{user?}", Headers(WatchGet))
|
||||
r.GET("/api/watchext/{user?}", Headers(WatchExtendedGet))
|
||||
r.POST("/api/appointment/{user}", Headers(AppointmentPost))
|
||||
r.POST("/api/chat/{id}/{user}", Headers(ChatPost))
|
||||
r.POST("/api/register", Headers(Register))
|
||||
r.POST("/api/watch/{user}", Headers(WatchPost))
|
||||
r.DELETE("/api/appointment/{user}", Headers(AppointmentDelete))
|
||||
r.DELETE("/api/register", Headers(UnRegister))
|
||||
r.DELETE("/api/watch/{user}", Headers(WatchDelete))
|
||||
log.Fatal(fasthttp.ListenAndServe(":"+strconv.Itoa(*webServerPort), r.Handler))
|
||||
@@ -395,6 +398,10 @@ func Register(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||
}
|
||||
|
||||
func AppointmentPost(ctx *fasthttp.RequestCtx) {
|
||||
processUpdateAppointmentReq(ctx, true)
|
||||
}
|
||||
|
||||
func ChatPost(ctx *fasthttp.RequestCtx) {
|
||||
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
|
||||
if ctx.UserValue("id") == nil || ctx.UserValue("user") == nil || auth == nil || string(auth) == "" || !strings.Contains(string(ctx.Request.Header.ContentType()), "text/plain") {
|
||||
@@ -445,6 +452,10 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
|
||||
processUpdateReq(ctx, true)
|
||||
}
|
||||
|
||||
func AppointmentDelete(ctx *fasthttp.RequestCtx) {
|
||||
processUpdateAppointmentReq(ctx, false)
|
||||
}
|
||||
|
||||
func UnRegister(ctx *fasthttp.RequestCtx) {
|
||||
if !strings.Contains(string(ctx.Request.Header.ContentType()), "application/json") {
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
@@ -597,9 +608,15 @@ func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) {
|
||||
ctx.SetStatusCode(fasthttp.StatusConflict)
|
||||
return
|
||||
}
|
||||
// anime is already dropped big baka user
|
||||
if listState == malApiStatusD {
|
||||
ctx.WriteString("Du hast schon gedropped bro")
|
||||
ctx.SetStatusCode(fasthttp.StatusConflict)
|
||||
return
|
||||
}
|
||||
animeData, err = AddUserToAnime(username, userId, anime.Anime, progress, updated)
|
||||
} else {
|
||||
// anime exitsts => delete
|
||||
// anime exists => delete
|
||||
animeData, err = DeleteUserFromAnime(username, userId, anime.Anime)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -622,6 +639,88 @@ func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) {
|
||||
ctx.SetContentType("application/json; charset=utf-8")
|
||||
}
|
||||
|
||||
func processUpdateAppointmentReq(ctx *fasthttp.RequestCtx, update bool) {
|
||||
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
|
||||
if ctx.UserValue("user") == nil || auth == nil || string(auth) == "" || !strings.Contains(string(ctx.Request.Header.ContentType()), "application/json") {
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
username := fmt.Sprintf("%s", ctx.UserValue("user"))
|
||||
legit, userId := GheddoAuth(username, string(auth))
|
||||
if !legit {
|
||||
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
body := ctx.PostBody()
|
||||
var appoints []Appointment
|
||||
err := json.Unmarshal(body, &appoints)
|
||||
if err != nil || len(appoints) == 0 {
|
||||
ctx.WriteString(err.Error())
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// iterate sent appointments
|
||||
for i, appointment := range appoints {
|
||||
var appData *Appointment
|
||||
var found bool
|
||||
|
||||
if update {
|
||||
// kann sich (noch) nicht in der Vergagenheit verabreden
|
||||
if appointment.Time.Before(time.Now()) {
|
||||
ctx.WriteString("kann sich (noch) nicht in der Vergagenheit verabreden")
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// check if user is a troll
|
||||
found, err = CheckAnimeExistInDbAndUserWatches(appointment.Anime, userId)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
ctx.WriteString("Anime gibts net oder du schaust net")
|
||||
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// save appointment and get list
|
||||
appData, err = AddUserToAppointment(username, appointment.Anime, appointment.Time)
|
||||
} else {
|
||||
found, err = CheckAnimeExistInDb(appointment.Anime)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
ctx.WriteString("Anime gibts net")
|
||||
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
||||
return
|
||||
}
|
||||
// anime exists => delete
|
||||
appData, err = DeleteUserFromAppointment(username, appointment.Anime, appointment.Time)
|
||||
}
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
appoints[i].Users = appData.Users
|
||||
}
|
||||
|
||||
data, err := json.Marshal(appoints)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
err = writeResponseBody(ctx, data)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
ctx.SetContentType("application/json; charset=utf-8")
|
||||
}
|
||||
|
||||
func writeResponseBody(ctx *fasthttp.RequestCtx, bytes []byte) error {
|
||||
_, err := ctx.Write(bytes)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user