90 lines
1.9 KiB
Go
Raw Normal View History

2016-11-09 11:32:19 +00:00
package main
import (
2017-06-08 19:54:39 +00:00
"errors"
"net/http"
2016-11-09 13:11:49 +00:00
"github.com/aerogo/aero"
2016-11-09 11:32:19 +00:00
"github.com/animenotifier/arn"
2017-06-08 19:54:39 +00:00
"github.com/animenotifier/notify.moe/utils"
2016-11-09 11:32:19 +00:00
)
func init() {
2016-11-19 14:54:31 +00:00
// app.Get("/all/anime", func(ctx *aero.Context) string {
// var titles []string
2016-11-09 11:32:19 +00:00
2016-11-19 14:54:31 +00:00
// results := make(chan *arn.Anime)
// arn.Scan("Anime", results)
2016-11-09 11:32:19 +00:00
2016-11-19 14:54:31 +00:00
// for anime := range results {
// titles = append(titles, anime.Title.Romaji)
// }
// sort.Strings(titles)
2016-11-09 11:32:19 +00:00
2016-11-20 10:26:11 +00:00
// return ctx.Error(toString(len(titles)) + "\n\n" + strings.Join(titles, "\n"))
2016-11-19 14:54:31 +00:00
// })
2016-11-09 11:32:19 +00:00
app.Get("/api/anime/:id", func(ctx *aero.Context) string {
2017-06-03 23:17:00 +00:00
id := ctx.Get("id")
2016-11-09 11:32:19 +00:00
anime, err := arn.GetAnime(id)
if err != nil {
2016-11-23 05:26:59 +00:00
return ctx.Error(404, "Anime not found", err)
2016-11-09 11:32:19 +00:00
}
return ctx.JSON(anime)
})
app.Get("/api/users/:nick", func(ctx *aero.Context) string {
nick := ctx.Get("nick")
user, err := arn.GetUserByNick(nick)
if err != nil {
2016-11-23 05:26:59 +00:00
return ctx.Error(404, "User not found", err)
2016-11-09 11:32:19 +00:00
}
return ctx.JSON(user)
})
2016-11-18 17:58:00 +00:00
app.Get("/api/threads/:id", func(ctx *aero.Context) string {
id := ctx.Get("id")
thread, err := arn.GetThread(id)
if err != nil {
2016-11-23 05:26:59 +00:00
return ctx.Error(404, "Thread not found", err)
2016-11-18 17:58:00 +00:00
}
return ctx.JSON(thread)
})
2017-06-08 19:54:39 +00:00
app.Get("/api/anime/:id/add", func(ctx *aero.Context) string {
animeID := ctx.Get("id")
user := utils.GetUser(ctx)
if user == nil {
return ctx.Error(http.StatusBadRequest, "Not logged in", errors.New("User not logged in"))
}
animeList := user.AnimeList()
if animeList.Contains(animeID) {
return ctx.Error(http.StatusBadRequest, "Anime already added", errors.New("Anime has already been added"))
}
newItem := arn.AnimeListItem{
AnimeID: animeID,
Status: arn.AnimeListStatusPlanned,
}
animeList.Items = append(animeList.Items, newItem)
saveError := animeList.Save()
if saveError != nil {
return ctx.Error(http.StatusInternalServerError, "Could not save anime list in database", saveError)
}
return ctx.JSON(animeList)
})
2016-11-09 11:32:19 +00:00
}