Minor updates

This commit is contained in:
Eduard Urbach 2017-06-10 02:19:31 +02:00
parent 36137f208f
commit c0744d8ba5
8 changed files with 194 additions and 106 deletions

158
api-old.go Normal file
View File

@ -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)
// // }

89
api.go
View File

@ -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)
})
}

View File

@ -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)

View File

@ -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")

View File

@ -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()

View File

@ -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))
}

View File

@ -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))
}

View File

@ -1,2 +1,5 @@
component Users
h2 Users
component Users(users []*arn.User)
h2.page-title Users
each user in users
p= user.Nick