Converted the last pieces to new backend
Added created & updated time to blog
This commit is contained in:
@@ -23,7 +23,7 @@ const (
|
|||||||
</tr></table>`
|
</tr></table>`
|
||||||
)
|
)
|
||||||
|
|
||||||
func latestBlogsHandle(w http.ResponseWriter, r *http.Request) {
|
func latestBlogsHandle(w http.ResponseWriter, _ *http.Request) {
|
||||||
latest, err := blogApp.LatestBlogs(0)
|
latest, err := blogApp.LatestBlogs(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == backend.ErrNotFound {
|
if err == backend.ErrNotFound {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func NewBackend(back *backend.Backend, db *mongo.Database) *CDRBackend {
|
|||||||
go func() {
|
go func() {
|
||||||
for range time.Tick(time.Hour) {
|
for range time.Tick(time.Hour) {
|
||||||
log.Println("CDR: Deleting expired dice")
|
log.Println("CDR: Deleting expired dice")
|
||||||
res, err := db.Collection("profiles").DeleteMany(context.TODO(), bson.M{"expiration": bson.M{"$lt": time.Now().Unix()}})
|
res, err := db.Collection("profiles").DeleteMany(context.Background(), bson.M{"expiration": bson.M{"$lt": time.Now().Unix()}})
|
||||||
if err == mongo.ErrNoDocuments {
|
if err == mongo.ErrNoDocuments {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ func (b CDRBackend) CrashTable() backend.CrashTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s CDRBackend) AddCrash(cr backend.IndividualCrash) bool {
|
func (s CDRBackend) AddCrash(cr backend.IndividualCrash) bool {
|
||||||
res := s.db.Collection("versions").FindOne(context.TODO(), bson.M{"version": cr.Version})
|
res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version})
|
||||||
return res.Err() != mongo.ErrNoDocuments
|
return res.Err() != mongo.ErrNoDocuments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-4
@@ -21,9 +21,13 @@ type UploadedDie struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b CDRBackend) UploadDie(w http.ResponseWriter, r *http.Request) {
|
func (b CDRBackend) UploadDie(w http.ResponseWriter, r *http.Request) {
|
||||||
hdr, err := b.back.VerifyHeader(w, r, "rooms", false)
|
hdr, err := b.back.VerifyHeader(w, r, "dice", false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if hdr.Key.AppID != "cdr" {
|
||||||
|
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application not authorized")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
backend.ReturnError(w, http.StatusBadRequest, "bad request", "Application sent a bad request")
|
backend.ReturnError(w, http.StatusBadRequest, "bad request", "Application sent a bad request")
|
||||||
@@ -52,7 +56,7 @@ func (b CDRBackend) UploadDie(w http.ResponseWriter, r *http.Request) {
|
|||||||
if toUpload.Die["uuid"] != nil {
|
if toUpload.Die["uuid"] != nil {
|
||||||
delete(toUpload.Die, "uuid")
|
delete(toUpload.Die, "uuid")
|
||||||
}
|
}
|
||||||
_, err = b.db.Collection("dice").InsertOne(context.TODO(), toUpload)
|
_, err = b.db.Collection("dice").InsertOne(context.Background(), toUpload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
log.Println("error inserting die:", err)
|
log.Println("error inserting die:", err)
|
||||||
@@ -63,7 +67,7 @@ func (b CDRBackend) UploadDie(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b CDRBackend) GetDie(w http.ResponseWriter, r *http.Request) {
|
func (b CDRBackend) GetDie(w http.ResponseWriter, r *http.Request) {
|
||||||
res := b.db.Collection("dice").FindOne(context.TODO(), bson.M{"_id": r.PathValue("dieID")})
|
res := b.db.Collection("dice").FindOne(context.Background(), bson.M{"_id": r.PathValue("dieID")})
|
||||||
if res.Err() == mongo.ErrNoDocuments {
|
if res.Err() == mongo.ErrNoDocuments {
|
||||||
backend.ReturnError(w, 404, "not found", "Die with the given id is not found")
|
backend.ReturnError(w, 404, "not found", "Die with the given id is not found")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func NewSWBackend(back *backend.Backend, db *mongo.Database) *SWBackend {
|
|||||||
go func() {
|
go func() {
|
||||||
for range time.Tick(time.Hour) {
|
for range time.Tick(time.Hour) {
|
||||||
log.Println("SWAssistant: Deleting expired profiles")
|
log.Println("SWAssistant: Deleting expired profiles")
|
||||||
res, err := db.Collection("profiles").DeleteMany(context.TODO(), bson.M{"expiration": bson.M{"$lt": time.Now().Unix()}})
|
res, err := db.Collection("profiles").DeleteMany(context.Background(), bson.M{"expiration": bson.M{"$lt": time.Now().Unix()}})
|
||||||
if err == mongo.ErrNoDocuments {
|
if err == mongo.ErrNoDocuments {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ func (s *SWBackend) CrashTable() backend.CrashTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SWBackend) AddCrash(cr backend.IndividualCrash) bool {
|
func (s *SWBackend) AddCrash(cr backend.IndividualCrash) bool {
|
||||||
res := s.db.Collection("versions").FindOne(context.TODO(), bson.M{"version": cr.Version})
|
res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version})
|
||||||
return res.Err() != mongo.ErrNoDocuments
|
return res.Err() != mongo.ErrNoDocuments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ func (s *SWBackend) UploadProfile(w http.ResponseWriter, r *http.Request) {
|
|||||||
Type: profType,
|
Type: profType,
|
||||||
Profile: prof,
|
Profile: prof,
|
||||||
}
|
}
|
||||||
_, err = s.db.Collection("profiles").InsertOne(context.TODO(), toUpload)
|
_, err = s.db.Collection("profiles").InsertOne(context.Background(), toUpload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
log.Println("error inserting profile:", err)
|
log.Println("error inserting profile:", err)
|
||||||
@@ -72,7 +72,7 @@ func (s *SWBackend) UploadProfile(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SWBackend) GetProfile(w http.ResponseWriter, r *http.Request) {
|
func (s *SWBackend) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||||
res := s.db.Collection("profiles").FindOne(context.TODO(), bson.M{"_id": r.PathValue("profileID")})
|
res := s.db.Collection("profiles").FindOne(context.Background(), bson.M{"_id": r.PathValue("profileID")})
|
||||||
if res.Err() == mongo.ErrNoDocuments {
|
if res.Err() == mongo.ErrNoDocuments {
|
||||||
backend.ReturnError(w, 404, "not found", "Profile not found")
|
backend.ReturnError(w, 404, "not found", "Profile not found")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func (s *SWBackend) ListRooms(w http.ResponseWriter, r *http.Request) {
|
|||||||
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application not authorized")
|
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application not authorized")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res, err := s.db.Collection("rooms").Find(context.TODO(), bson.M{"users": hdr.User.Username}, options.Find().SetProjection(bson.M{"_id": 1, "name": 1, "owner": 1}))
|
res, err := s.db.Collection("rooms").Find(context.Background(), bson.M{"users": hdr.User.Username}, options.Find().SetProjection(bson.M{"_id": 1, "name": 1, "owner": 1}))
|
||||||
if err != nil && err != mongo.ErrNoDocuments {
|
if err != nil && err != mongo.ErrNoDocuments {
|
||||||
log.Println("error getting room list:", err)
|
log.Println("error getting room list:", err)
|
||||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
@@ -42,7 +42,7 @@ func (s *SWBackend) ListRooms(w http.ResponseWriter, r *http.Request) {
|
|||||||
Owner string `json:"owner" bson:"owner"`
|
Owner string `json:"owner" bson:"owner"`
|
||||||
}, 0)
|
}, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = res.All(context.TODO(), &out)
|
err = res.All(context.Background(), &out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error decoding room list:", err)
|
log.Println("error decoding room list:", err)
|
||||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
@@ -61,40 +61,26 @@ func (s *SWBackend) NewRoom(w http.ResponseWriter, r *http.Request) {
|
|||||||
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application not authorized")
|
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application not authorized")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.Method != http.MethodPost || req.Query["name"] == nil || len(req.Query["name"]) != 1 || req.Query["name"][0] == "" {
|
name := r.URL.Query().Get("name")
|
||||||
req.Resp.WriteHeader(http.StatusBadRequest)
|
if name == "" {
|
||||||
return true
|
backend.ReturnError(w, http.StatusBadRequest, "bad request", "Application sent bad request")
|
||||||
} else if req.User == nil {
|
return
|
||||||
req.Resp.WriteHeader(http.StatusUnauthorized)
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
//TODO: check room name for unsavory words
|
//TODO: check room name for unsavory words
|
||||||
newRoom := Room{
|
newRoom := Room{
|
||||||
ID: uuid.NewString(),
|
ID: uuid.NewString(),
|
||||||
Name: req.Query["name"][0],
|
Name: name,
|
||||||
Owner: req.User.Username,
|
Owner: hdr.User.Username,
|
||||||
Users: []string{},
|
Users: []string{},
|
||||||
Profiles: []string{},
|
Profiles: []string{},
|
||||||
}
|
}
|
||||||
_, err := s.db.Collection("rooms").InsertOne(context.TODO(), newRoom)
|
_, err = s.db.Collection("rooms").InsertOne(context.Background(), newRoom)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("SWAssistant: Error creating room:", err)
|
log.Println("error creating room:", err)
|
||||||
req.Resp.WriteHeader(http.StatusInternalServerError)
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return true
|
return
|
||||||
}
|
}
|
||||||
out, err := json.Marshal(map[string]string{"id": newRoom.ID, "name": newRoom.Name})
|
json.NewEncoder(w).Encode(map[string]string{"id": newRoom.ID, "name": newRoom.Name})
|
||||||
if err != nil {
|
|
||||||
log.Println("SWAssistant: Error encoding new room:", err)
|
|
||||||
req.Resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
req.Resp.WriteHeader(http.StatusCreated)
|
|
||||||
_, err = req.Resp.Write(out)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("SWAssistant: Error writing new room:", err)
|
|
||||||
req.Resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SWBackend) GetRoom(w http.ResponseWriter, r *http.Request) {
|
func (s *SWBackend) GetRoom(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -106,39 +92,22 @@ func (s *SWBackend) GetRoom(w http.ResponseWriter, r *http.Request) {
|
|||||||
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application not authorized")
|
backend.ReturnError(w, http.StatusUnauthorized, "unauthorized", "Application not authorized")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.Method != http.MethodGet {
|
roomID := r.PathValue("roomID")
|
||||||
req.Resp.WriteHeader(http.StatusBadRequest)
|
res := s.db.Collection("rooms").FindOne(context.TODO(), bson.M{"_id": roomID})
|
||||||
return true
|
|
||||||
} else if req.User == nil {
|
|
||||||
req.Resp.WriteHeader(http.StatusUnauthorized)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
res := s.db.Collection("rooms").FindOne(context.TODO(), bson.M{"_id": req.Path[1]})
|
|
||||||
if res.Err() == mongo.ErrNoDocuments {
|
if res.Err() == mongo.ErrNoDocuments {
|
||||||
req.Resp.WriteHeader(http.StatusNotFound)
|
backend.ReturnError(w, http.StatusNotFound, "not found", "Room not found")
|
||||||
return true
|
return
|
||||||
} else if res.Err() != nil {
|
} else if res.Err() != nil {
|
||||||
log.Println("SWAssistant: Error getting room:", res.Err())
|
log.Println("error getting room:", res.Err())
|
||||||
req.Resp.WriteHeader(http.StatusInternalServerError)
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return true
|
return
|
||||||
}
|
}
|
||||||
r := Room{}
|
var rm Room
|
||||||
err := res.Decode(&r)
|
err = res.Decode(&rm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("SWAssistant: Error decoding room:", err)
|
log.Println("error decoding room:", err)
|
||||||
req.Resp.WriteHeader(http.StatusInternalServerError)
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return true
|
return
|
||||||
}
|
}
|
||||||
out, err := json.Marshal(r)
|
json.NewEncoder(w).Encode(rm)
|
||||||
if err != nil {
|
|
||||||
log.Println("SWAssistant: Error encoding room:", err)
|
|
||||||
req.Resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
_, err = req.Resp.Write(out)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("SWAssistant: Error writing room:", err)
|
|
||||||
req.Resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user