func CreateUserHandler(c jakiro.Context) { user := new(User) err := user.FromJSON(c.Body()) if err != nil { // 400 Bad Request c.Error(400, err) return } // Insert the user. user.Id = int64(len(DBUsersTable) + 1) user.CreatedAt = time.Now() DBUsersTable[user.Id] = user // 201 Created c.JSON(201, user) }
func ReadUserHandler(c jakiro.Context) { idStr := c.Params()["id"] id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { // 500 Internal Server Error c.Error(500, err) return } // Select the user. user, exists := DBUsersTable[id] if exists == false { c.Error(404, errors.New("User not found")) return } // 200 OK c.JSON(200, user) return }
func DestroyUserHandler(c jakiro.Context) { idStr := c.Params()["id"] id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { // 500 Internal Server Error c.Error(500, err) return } _, exists := DBUsersTable[id] if exists == false { c.Error(404, errors.New("User not found")) return } // Delete the user. delete(DBUsersTable, id) // 202 Accepted c.Write(202, nil) return }