// CreateCard creates a new card entry. func CreateCard(w rest.ResponseWriter, r *rest.Request) { var err error c, err := card.New() if err != nil { log.Printf("CreateCard: Failed to create card: %s", err) rest.Error(w, "Failed to create card", 500) return } decoder := json.NewDecoder(r.Body) err = decoder.Decode(&c) if err != nil { log.Printf("CreateCard: Failed to decode request body: %s", err) rest.Error(w, "Failed to decode request body", 500) return } if err := card.GetHandler().SaveCard(*c); err != nil { log.Printf("CreateCard: Failed to save card: %s", err) rest.Error(w, "Failed to create card", 500) return } log.Printf("CreateCard: Card save was successful (%s)", c.Cid) w.WriteJson(c) }
// DeleteCard delete card by id. func DeleteCard(w rest.ResponseWriter, r *rest.Request) { cid := r.PathParam("cid") if cid == "" { log.Println("DeleteCard: Card id is missing") rest.Error(w, "Card delete failed", 422) return } err := card.GetHandler().DeleteCard(cid) if err != nil { log.Printf("DeleteCard: Failed to delete card (cid: %s): %s", cid, err) rest.Error(w, "Card delete failed", 404) return } log.Printf("DeleteCard: Card delete was successful (%s)", cid) }
// GetCard retrieve card by id. func GetCard(w rest.ResponseWriter, r *rest.Request) { cid := r.PathParam("cid") if cid == "" { log.Println("GetCard: Card id is missing") rest.Error(w, "Failed to retrieve card", 422) return } c, err := card.GetHandler().RetrieveCard(cid) if err != nil { log.Printf("GetCard: Failed to load card (cid: %s): %s", cid, err) rest.Error(w, "Failed to retrieve card", 404) return } log.Printf("GetCard: Retrieve card was successful (%s)", cid) w.WriteJson(&c) }
func init() { var err error con := app.GetDBConnection() if err != nil { log.Fatal(err) } ch := card.GetHandler() rh := result.GetHandler() h := SpacedRepetitionLearner{con, rh, ch} err = h.createSRTable() if err != nil { log.Fatal(err) } srLearner = &h }
// GetUserCards returns all the cards that belongs to the given user. func GetUserCards(w rest.ResponseWriter, r *rest.Request) { // var ok bool // var uid string // if uid, ok = r.Env["User"].(string); !ok { // log.Println("GetUserCards: Card id is missing") // rest.Error(w, "Failed to retrieve card", 422) // return // } cards, err := card.GetHandler().RetrieveUserCards() if err != nil { log.Printf("GetUserCards: Failed to load user cards: %s", err) rest.Error(w, "Failed to load user cards", 500) return } w.WriteJson(&cards) }
// UpdateCard card by card id. func UpdateCard(w rest.ResponseWriter, r *rest.Request) { var err error var c *card.Card ur := new(card.UpdateRequest) if err != nil { log.Printf("UpdateCard: Card update failed: %s", err) rest.Error(w, "Card update failed", 500) return } decoder := json.NewDecoder(r.Body) err = decoder.Decode(&ur) if err != nil { log.Printf("UpdateCard: Failed to decode request body: %s", err) rest.Error(w, "Failed to decode request body", 500) return } if ur.Cid == "" { log.Println("UpdateCard: Missing card id") rest.Error(w, "Missing card id", 422) return } if c, err = ur.ToCard(); err != nil { log.Println("UpdateCard: Failed to create card object") rest.Error(w, "Card update failed", 500) return } log.Printf("UpdateCard: Card before update (%+v)", c) if err := card.GetHandler().SaveCard(*c); err != nil { log.Printf("UpdateCard: Card update failed: %s", err) rest.Error(w, "Card update failed", 500) return } log.Printf("UpdateCard: Card update was successful (%s)", c.Cid) w.WriteJson(c) }
// SaveResult Saves the result of a recall. func SaveResult(w rest.ResponseWriter, r *rest.Request) { res, err := result.New() if err != nil { log.Printf("SaveResult: Failed to save result: %s", err) rest.Error(w, "Failed to save result", 500) return } decoder := json.NewDecoder(r.Body) err = decoder.Decode(&res) if err != nil { log.Printf("SaveResult: Failed to decode request body: %s", err) rest.Error(w, "Failed to decode request body", 500) return } card, err := card.GetHandler().RetrieveCard(res.Cid) if err != nil || card == nil { log.Printf("SaveResult: Failed to retrieve card with cid: %s. %s", res.Cid, err) rest.Error(w, "Failed to save result", 500) return } if err := result.GetHandler().SaveResult(*res); err != nil { log.Printf("SaveResult: Failed to save result: %s", err) rest.Error(w, "Failed to save result", 500) return } if err := learning.SaveLearnOutcome(*res); err != nil { log.Printf("SaveResult: Failed to save result: %s", err) rest.Error(w, "Failed to save result", 500) } log.Println("SaveResult: Result save was successful") w.WriteJson(res) }