func GetChannel(w rest.ResponseWriter, req *rest.Request) { requesterID := GetRequesterID(req) channelname := req.PathParam("channelname") channel, httpErr := transactions.GetChannel(requesterID, channelname) if httpErr != nil { log.Println(httpErr) rest.Error(w, httpErr.Error(), httpErr.Code()) return } w.WriteJson(channel) w.WriteHeader(http.StatusOK) }
func AddChannel(w rest.ResponseWriter, req *rest.Request) { requesterID := GetRequesterID(req) var jsonBody json.RawMessage err := req.DecodeJsonPayload(&jsonBody) if err != nil { log.Println("invalid json body") rest.Error(w, err.Error(), http.StatusBadRequest) return } channel, err := types.UnmarshalChannel(jsonBody) if err != nil { log.Println("invalid channel structure", err) rest.Error(w, err.Error(), http.StatusBadRequest) return } channel.CreatorID = requesterID err = channel.Validate() if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusBadRequest) return } httpErr := transactions.AddChannel(channel) if httpErr != nil { log.Println(httpErr) rest.Error(w, httpErr.Error(), httpErr.Code()) return } insertedChannel, httpErr := transactions.GetChannel(requesterID, channel.Name) if httpErr != nil { log.Println(httpErr) rest.Error(w, httpErr.Error(), httpErr.Code()) return } w.WriteJson(insertedChannel) w.WriteHeader(http.StatusOK) }
func AddPost(w rest.ResponseWriter, req *rest.Request) { requesterID := GetRequesterID(req) var jsonBody json.RawMessage err := req.DecodeJsonPayload(&jsonBody) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } submissionChannel := struct { Channel string `json:"channel"` }{} err = json.Unmarshal(jsonBody, &submissionChannel) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } channel, err := transactions.GetChannel(requesterID, submissionChannel.Channel) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } post, err := channel.UnmarshalSubmissionToPost(jsonBody) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } err = post.Validate() if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } post.CreatorID = requesterID post.ID = base64.RawURLEncoding.EncodeToString(uuid.NewRandom()) post.Time = time.Now().UTC() err = post.GenerateThumbnail() if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } err = transactions.AddPost(requesterID, post) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } insertedPost, err := transactions.GetPost(requesterID, post.ID) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteJson(insertedPost) w.WriteHeader(http.StatusOK) }