예제 #1
0
func IndexUserHandler(c jakiro.Context) {
	users := make(Users, 0, len(DBUsersTable))

	var keys []int
	for k := range DBUsersTable {
		keys = append(keys, int(k))
	}

	sort.Ints(keys)

	// Select users sorted by ID.
	for _, k := range keys {
		users = append(users, DBUsersTable[int64(k)])
	}

	c.JSON(200, users)
	return
}
예제 #2
0
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
}
예제 #3
0
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
}
예제 #4
0
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)
}