From c0744d8ba5042a78cb8e333b9044eac65a89355d Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 10 Jun 2017 02:19:31 +0200 Subject: [PATCH] Minor updates --- api-old.go | 158 +++++++++++++++++++++++++++++++++++++++ api.go | 89 ---------------------- main.go | 8 +- mixins/Navigation.pixy | 2 +- pages/airing/airing.go | 2 +- pages/profile/profile.go | 21 ++++-- pages/users/users.go | 13 +++- pages/users/users.pixy | 7 +- 8 files changed, 194 insertions(+), 106 deletions(-) create mode 100644 api-old.go delete mode 100644 api.go diff --git a/api-old.go b/api-old.go new file mode 100644 index 00000000..8820c112 --- /dev/null +++ b/api-old.go @@ -0,0 +1,158 @@ +package main + +// import ( +// "errors" +// "net/http" + +// "github.com/aerogo/aero" +// "github.com/animenotifier/arn" +// "github.com/animenotifier/notify.moe/utils" +// ) + +// func init() { +// // app.Get("/all/anime", func(ctx *aero.Context) string { +// // var titles []string + +// // results := make(chan *arn.Anime) +// // arn.Scan("Anime", results) + +// // for anime := range results { +// // titles = append(titles, anime.Title.Romaji) +// // } +// // sort.Strings(titles) + +// // return ctx.Error(toString(len(titles)) + "\n\n" + strings.Join(titles, "\n")) +// // }) + +// app.Get("/api/anime/:id", func(ctx *aero.Context) string { +// id := ctx.Get("id") +// anime, err := arn.GetAnime(id) + +// if err != nil { +// return ctx.Error(404, "Anime not found", err) +// } + +// 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 { +// return ctx.Error(404, "User not found", err) +// } + +// return ctx.JSON(user) +// }) + +// app.Get("/api/threads/:id", func(ctx *aero.Context) string { +// id := ctx.Get("id") +// thread, err := arn.GetThread(id) + +// if err != nil { +// return ctx.Error(404, "Thread not found", err) +// } + +// return ctx.JSON(thread) +// }) + +// animeListAPI := &AnimeListAPI{} +// app.Get("/api/anime/:id/add", APIAdd(animeListAPI)) +// } + +// type AnimeListAPI struct { +// } + +// func (api *AnimeListAPI) Key(user *arn.User) interface{} { +// return user.ID +// } + +// func (api *AnimeListAPI) Table() string { +// return "AnimeList" +// } + +// func (api *AnimeListAPI) NewList() EasyAPIList { +// return new(arn.AnimeList) +// } + +// func (api *AnimeListAPI) ListItemID(ctx *aero.Context) string { +// return ctx.Get("id") +// } + +// // EasyAPI +// type EasyAPI interface { +// Key(*arn.User) interface{} +// Table() string +// NewList() GenericList +// ListItemID(*aero.Context) string +// } + +// // GenericList ... +// type GenericList interface { +// Add(string) error +// Save() error +// } + +// // APIAdd ... +// func APIAdd(api EasyAPI) aero.Handle { +// return func(ctx *aero.Context) string { +// objectID := api.ListItemID(ctx) + +// // Auth +// user := utils.GetUser(ctx) + +// if user == nil { +// return ctx.Error(http.StatusBadRequest, "Not logged in", errors.New("User not logged in")) +// } + +// list := api.NewList() + +// // Fetch list +// arn.GetObject(api.Table(), api.Key(user), list) + +// // Add +// addError := list.Add(objectID) + +// if addError != nil { +// return ctx.Error(http.StatusBadRequest, addError.Error(), addError) +// } + +// // Save +// saveError := list.Save() + +// if saveError != nil { +// return ctx.Error(http.StatusInternalServerError, "Could not save anime list in database", saveError) +// } + +// // Respond +// return ctx.JSON(list) +// } +// } + +// // 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() + +// // // Add +// // addError := animeList.Add(animeID) + +// // if addError != nil { +// // return ctx.Error(http.StatusBadRequest, "Failed adding anime", addError) +// // } + +// // // Save +// // saveError := animeList.Save() + +// // if saveError != nil { +// // return ctx.Error(http.StatusInternalServerError, "Could not save anime list in database", saveError) +// // } + +// // return ctx.JSON(animeList) +// // } diff --git a/api.go b/api.go deleted file mode 100644 index 8e47506a..00000000 --- a/api.go +++ /dev/null @@ -1,89 +0,0 @@ -package main - -import ( - "errors" - "net/http" - - "github.com/aerogo/aero" - "github.com/animenotifier/arn" - "github.com/animenotifier/notify.moe/utils" -) - -func init() { - // app.Get("/all/anime", func(ctx *aero.Context) string { - // var titles []string - - // results := make(chan *arn.Anime) - // arn.Scan("Anime", results) - - // for anime := range results { - // titles = append(titles, anime.Title.Romaji) - // } - // sort.Strings(titles) - - // return ctx.Error(toString(len(titles)) + "\n\n" + strings.Join(titles, "\n")) - // }) - - app.Get("/api/anime/:id", func(ctx *aero.Context) string { - id := ctx.Get("id") - anime, err := arn.GetAnime(id) - - if err != nil { - return ctx.Error(404, "Anime not found", err) - } - - 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 { - return ctx.Error(404, "User not found", err) - } - - return ctx.JSON(user) - }) - - app.Get("/api/threads/:id", func(ctx *aero.Context) string { - id := ctx.Get("id") - thread, err := arn.GetThread(id) - - if err != nil { - return ctx.Error(404, "Thread not found", err) - } - - return ctx.JSON(thread) - }) - - 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) - }) -} diff --git a/main.go b/main.go index 6fbabb12..6128679a 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,6 @@ import ( "github.com/animenotifier/notify.moe/pages/dashboard" "github.com/animenotifier/notify.moe/pages/forum" "github.com/animenotifier/notify.moe/pages/forums" - "github.com/animenotifier/notify.moe/pages/genre" - "github.com/animenotifier/notify.moe/pages/genres" "github.com/animenotifier/notify.moe/pages/posts" "github.com/animenotifier/notify.moe/pages/profile" "github.com/animenotifier/notify.moe/pages/search" @@ -59,16 +57,16 @@ func main() { app.Ajax("/", dashboard.Get) app.Ajax("/anime", search.Get) app.Ajax("/anime/:id", anime.Get) - app.Ajax("/genres", genres.Get) - app.Ajax("/genres/:name", genre.Get) + // app.Ajax("/genres", genres.Get) + // app.Ajax("/genres/:name", genre.Get) app.Ajax("/forum", forums.Get) app.Ajax("/forum/:tag", forum.Get) app.Ajax("/threads/:id", threads.Get) app.Ajax("/posts/:id", posts.Get) app.Ajax("/user/:nick", profile.Get) app.Ajax("/user/:nick/threads", threads.GetByUser) - app.Ajax("/airing", airing.Get) app.Ajax("/users", users.Get) + app.Ajax("/airing", airing.Get) app.Ajax("/awards", awards.Get) EnableGoogleLogin(app) diff --git a/mixins/Navigation.pixy b/mixins/Navigation.pixy index b5a4ead8..e9a48da9 100644 --- a/mixins/Navigation.pixy +++ b/mixins/Navigation.pixy @@ -3,7 +3,7 @@ component Navigation NavigationButton("Dash", "/", "inbox") NavigationButton("Anime", "/anime", "television") NavigationButton("Forum", "/forum", "comment") - NavigationButton("Genres", "/genres", "tags") + NavigationButton("Users", "/users", "globe") NavigationButton("Airing", "/airing", "rss") //- NavigationButton("Users", "/users", "globe") diff --git a/pages/airing/airing.go b/pages/airing/airing.go index 13173569..05f68638 100644 --- a/pages/airing/airing.go +++ b/pages/airing/airing.go @@ -9,7 +9,7 @@ import ( // Get ... func Get(ctx *aero.Context) string { var cache arn.ListOfIDs - err := arn.GetObject("Cache", "airing anime", &cache) + err := arn.DB.GetObject("Cache", "airing anime", &cache) airing, err := arn.GetAiringAnimeCached() diff --git a/pages/profile/profile.go b/pages/profile/profile.go index d55977f1..a7346e2c 100644 --- a/pages/profile/profile.go +++ b/pages/profile/profile.go @@ -13,21 +13,28 @@ const maxPosts = 5 func Get(ctx *aero.Context) string { nick := ctx.Get("nick") viewUser, err := arn.GetUserByNick(nick) - user := utils.GetUser(ctx) if err != nil { return ctx.Error(404, "User not found", err) } - threads := viewUser.Threads() + var user *arn.User + var threads []*arn.Thread + var animeList *arn.AnimeList - arn.SortThreadsByDate(threads) + aero.Async(func() { + user = utils.GetUser(ctx) + }, func() { + animeList = viewUser.AnimeList() + }, func() { + threads = viewUser.Threads() - if len(threads) > maxPosts { - threads = threads[:maxPosts] - } + arn.SortThreadsByDate(threads) - animeList := viewUser.AnimeList() + if len(threads) > maxPosts { + threads = threads[:maxPosts] + } + }) return ctx.HTML(components.Profile(viewUser, user, animeList, threads)) } diff --git a/pages/users/users.go b/pages/users/users.go index 0fa50741..a6cd8e35 100644 --- a/pages/users/users.go +++ b/pages/users/users.go @@ -1,11 +1,22 @@ package users import ( + "net/http" + "github.com/aerogo/aero" + "github.com/animenotifier/arn" "github.com/animenotifier/notify.moe/components" ) // Get ... func Get(ctx *aero.Context) string { - return ctx.HTML(components.Users()) + users, err := arn.FilterUsers(func(user *arn.User) bool { + return user.IsActive() + }) + + if err != nil { + return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err) + } + + return ctx.HTML(components.Users(users)) } diff --git a/pages/users/users.pixy b/pages/users/users.pixy index cc4abc8d..b62fa142 100644 --- a/pages/users/users.pixy +++ b/pages/users/users.pixy @@ -1,2 +1,5 @@ -component Users - h2 Users \ No newline at end of file +component Users(users []*arn.User) + h2.page-title Users + + each user in users + p= user.Nick \ No newline at end of file