Refactor to use aerogo/database

This commit is contained in:
Eduard Urbach 2017-10-27 09:11:56 +02:00
parent c0d7b0d2df
commit b07a98ed32
18 changed files with 52 additions and 164 deletions

View File

@ -92,11 +92,7 @@ func InstallFacebookAuth(app *aero.Application) {
if user != nil { if user != nil {
// Add FacebookToUser reference // Add FacebookToUser reference
err = user.ConnectFacebook(fbUser.ID) user.ConnectFacebook(fbUser.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not connect account to Facebook account", err)
}
// Save in DB // Save in DB
user.Save() user.Save()
@ -110,7 +106,7 @@ func InstallFacebookAuth(app *aero.Application) {
var getErr error var getErr error
// Try to find an existing user via the Facebook user ID // Try to find an existing user via the Facebook user ID
user, getErr = arn.GetUserFromTable("FacebookToUser", fbUser.ID) user, getErr = arn.GetUserByFacebookID(fbUser.ID)
if getErr == nil && user != nil { if getErr == nil && user != nil {
authLog.Info("User logged in via Facebook ID", user.ID, user.Nick, ctx.RealIP(), user.Email, user.RealName()) authLog.Info("User logged in via Facebook ID", user.ID, user.Nick, ctx.RealIP(), user.Email, user.RealName())
@ -148,18 +144,10 @@ func InstallFacebookAuth(app *aero.Application) {
user.Save() user.Save()
// Register user // Register user
err = arn.RegisterUser(user) arn.RegisterUser(user)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not register a new user", err)
}
// Connect account to a Facebook account // Connect account to a Facebook account
err = user.ConnectFacebook(fbUser.ID) user.ConnectFacebook(fbUser.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not connect account to Facebook account", err)
}
// Save user object again with updated data // Save user object again with updated data
user.Save() user.Save()

View File

@ -102,11 +102,7 @@ func InstallGoogleAuth(app *aero.Application) {
if user != nil { if user != nil {
// Add GoogleToUser reference // Add GoogleToUser reference
err = user.ConnectGoogle(googleUser.Sub) user.ConnectGoogle(googleUser.Sub)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not connect account to Google account", err)
}
// Save in DB // Save in DB
user.Save() user.Save()
@ -120,7 +116,7 @@ func InstallGoogleAuth(app *aero.Application) {
var getErr error var getErr error
// Try to find an existing user via the Google user ID // Try to find an existing user via the Google user ID
user, getErr = arn.GetUserFromTable("GoogleToUser", googleUser.Sub) user, getErr = arn.GetUserByGoogleID(googleUser.Sub)
if getErr == nil && user != nil { if getErr == nil && user != nil {
authLog.Info("User logged in via Google ID", user.ID, user.Nick, ctx.RealIP(), user.Email, user.RealName()) authLog.Info("User logged in via Google ID", user.ID, user.Nick, ctx.RealIP(), user.Email, user.RealName())
@ -158,18 +154,10 @@ func InstallGoogleAuth(app *aero.Application) {
user.Save() user.Save()
// Register user // Register user
err = arn.RegisterUser(user) arn.RegisterUser(user)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not register a new user", err)
}
// Connect account to a Google account // Connect account to a Google account
err = user.ConnectGoogle(googleUser.Sub) user.ConnectGoogle(googleUser.Sub)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not connect account to Google account", err)
}
// Save user object again with updated data // Save user object again with updated data
user.Save() user.Save()

View File

@ -2,7 +2,6 @@ package main
import ( import (
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/aerogo/session-store-aerospike"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/auth" "github.com/animenotifier/notify.moe/auth"
"github.com/animenotifier/notify.moe/components/css" "github.com/animenotifier/notify.moe/components/css"
@ -69,7 +68,10 @@ func configure(app *aero.Application) *aero.Application {
// Sessions // Sessions
app.Sessions.Duration = 3600 * 24 * 30 * 6 app.Sessions.Duration = 3600 * 24 * 30 * 6
app.Sessions.Store = aerospikestore.New(arn.DB, "Session", app.Sessions.Duration)
// TODO: ...
println("Using memory session store")
// app.Sessions.Store = aerospikestore.New(arn.DB, "Session", app.Sessions.Duration)
// Layout // Layout
app.Layout = layout.Render app.Layout = layout.Render
@ -210,8 +212,6 @@ func configure(app *aero.Application) *aero.Application {
// Domain // Domain
if arn.IsDevelopment() { if arn.IsDevelopment() {
app.Config.Domain = "beta.notify.moe" app.Config.Domain = "beta.notify.moe"
} else {
arn.DB.SetScanPriority("high")
} }
// Authentication // Authentication

View File

@ -48,11 +48,7 @@ func Select(ctx *aero.Context) string {
Results: []interface{}{}, Results: []interface{}{},
} }
stream, err := arn.DB.All(dataTypeName) stream := arn.DB.All(dataTypeName)
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error fetching data from the database", err)
}
process := func(obj interface{}) { process := func(obj interface{}) {
_, _, value, _ := mirror.GetField(obj, field) _, _, value, _ := mirror.GetField(obj, field)
@ -62,48 +58,9 @@ func Select(ctx *aero.Context) string {
} }
} }
switch dataTypeName { for obj := range stream {
case "Analytics":
for obj := range stream.(chan *arn.Analytics) {
process(obj) process(obj)
} }
case "Anime":
for obj := range stream.(chan *arn.Anime) {
process(obj)
}
case "AnimeList":
for obj := range stream.(chan *arn.AnimeList) {
process(obj)
}
case "Character":
for obj := range stream.(chan *arn.Character) {
process(obj)
}
case "Group":
for obj := range stream.(chan *arn.Group) {
process(obj)
}
case "Post":
for obj := range stream.(chan *arn.Post) {
process(obj)
}
case "Settings":
for obj := range stream.(chan *arn.Settings) {
process(obj)
}
case "SoundTrack":
for obj := range stream.(chan *arn.SoundTrack) {
process(obj)
}
case "Thread":
for obj := range stream.(chan *arn.Thread) {
process(obj)
}
case "User":
for obj := range stream.(chan *arn.User) {
process(obj)
}
}
for _, obj := range response.Results { for _, obj := range response.Results {
mirror.GetField(obj, field) mirror.GetField(obj, field)

View File

@ -1,7 +1,6 @@
package editor package editor
import ( import (
"net/http"
"sort" "sort"
"github.com/aerogo/aero" "github.com/aerogo/aero"
@ -13,14 +12,10 @@ const maxAniListEntries = 70
// AniList ... // AniList ...
func AniList(ctx *aero.Context) string { func AniList(ctx *aero.Context) string {
missing, err := arn.FilterAnime(func(anime *arn.Anime) bool { missing := arn.FilterAnime(func(anime *arn.Anime) bool {
return anime.GetMapping("anilist/anime") == "" return anime.GetMapping("anilist/anime") == ""
}) })
if err != nil {
ctx.Error(http.StatusInternalServerError, "Couldn't filter anime", err)
}
sort.Slice(missing, func(i, j int) bool { sort.Slice(missing, func(i, j int) bool {
a := missing[i] a := missing[i]
b := missing[j] b := missing[j]

View File

@ -1,7 +1,6 @@
package editor package editor
import ( import (
"net/http"
"sort" "sort"
"github.com/aerogo/aero" "github.com/aerogo/aero"
@ -13,14 +12,10 @@ const maxShoboiEntries = 70
// Shoboi ... // Shoboi ...
func Shoboi(ctx *aero.Context) string { func Shoboi(ctx *aero.Context) string {
missing, err := arn.FilterAnime(func(anime *arn.Anime) bool { missing := arn.FilterAnime(func(anime *arn.Anime) bool {
return anime.GetMapping("shoboi/anime") == "" return anime.GetMapping("shoboi/anime") == ""
}) })
if err != nil {
ctx.Error(http.StatusInternalServerError, "Couldn't filter anime", err)
}
sort.Slice(missing, func(i, j int) bool { sort.Slice(missing, func(i, j int) bool {
a := missing[i] a := missing[i]
b := missing[j] b := missing[j]

View File

@ -2,20 +2,19 @@ package explore
import ( import (
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
) )
// Get ... // Get ...
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
var cache arn.ListOfIDs // var cache arn.ListOfIDs
err := arn.DB.GetObject("Cache", "airing anime", &cache) // err := arn.DB.GetObject("Cache", "airing anime", &cache)
airing, err := arn.GetAiringAnimeCached() // airing, err := arn.GetAiringAnimeCached()
if err != nil { // if err != nil {
return ctx.Error(500, "Couldn't fetch airing anime", err) // return ctx.Error(500, "Couldn't fetch airing anime", err)
} // }
return ctx.HTML(components.Airing(airing)) // return ctx.HTML(components.Airing(airing))
return ctx.HTML("Not implemented")
} }

View File

@ -12,7 +12,7 @@ const ThreadsPerPage = 20
// Get forum category. // Get forum category.
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
tag := ctx.Get("tag") tag := ctx.Get("tag")
threads, _ := arn.GetThreadsByTag(tag) threads := arn.GetThreadsByTag(tag)
arn.SortThreads(threads) arn.SortThreads(threads)
if len(threads) > ThreadsPerPage { if len(threads) > ThreadsPerPage {

View File

@ -64,11 +64,7 @@ func Finish(ctx *aero.Context) string {
animeList.Import(item) animeList.Import(item)
} }
err := animeList.Save() animeList.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error saving your anime list", err)
}
return ctx.Redirect("/+" + user.Nick + "/animelist") return ctx.Redirect("/+" + user.Nick + "/animelist")
} }

View File

@ -77,11 +77,7 @@ func Finish(ctx *aero.Context) string {
animeList.Import(item) animeList.Import(item)
} }
err := animeList.Save() animeList.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error saving your anime list", err)
}
return ctx.Redirect("/+" + user.Nick + "/animelist") return ctx.Redirect("/+" + user.Nick + "/animelist")
} }

View File

@ -73,11 +73,7 @@ func Finish(ctx *aero.Context) string {
animeList.Import(item) animeList.Import(item)
} }
err := animeList.Save() animeList.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error saving your anime list", err)
}
return ctx.Redirect("/+" + user.Nick + "/animelist") return ctx.Redirect("/+" + user.Nick + "/animelist")
} }

View File

@ -66,21 +66,13 @@ func Success(ctx *aero.Context) string {
Created: arn.DateTimeUTC(), Created: arn.DateTimeUTC(),
} }
err = payment.Save() payment.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not save payment in the database", err)
}
// Increase user's balance // Increase user's balance
user.Balance += payment.Gems() user.Balance += payment.Gems()
// Save in DB // Save in DB
err = user.Save() user.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not save new balance", err)
}
// Notify admin // Notify admin
go func() { go func() {

View File

@ -46,27 +46,16 @@ func BuyItem(ctx *aero.Context) string {
} }
user.Balance -= totalPrice user.Balance -= totalPrice
err = user.Save() user.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error saving user data", err)
}
// Add item to user inventory // Add item to user inventory
inventory := user.Inventory() inventory := user.Inventory()
inventory.AddItem(itemID, uint(quantity)) inventory.AddItem(itemID, uint(quantity))
err = inventory.Save() inventory.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error saving inventory", err)
}
// Save purchase // Save purchase
err = arn.NewPurchase(user.ID, itemID, quantity, int(item.Price), "gem").Save() purchase := arn.NewPurchase(user.ID, itemID, quantity, int(item.Price), "gem")
purchase.Save()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error saving purchase", err)
}
return "ok" return "ok"
} }

View File

@ -2,13 +2,12 @@ package statistics
import ( import (
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
) )
// Anime ... // Anime ...
func Anime(ctx *aero.Context) string { func Anime(ctx *aero.Context) string {
statistics := arn.StatisticsCategory{} // statistics := arn.StatisticsCategory{}
arn.DB.GetObject("Cache", "anime statistics", &statistics) // arn.DB.GetObject("Cache", "anime statistics", &statistics)
return ctx.HTML(components.Statistics(statistics.PieCharts...)) // return ctx.HTML(components.Statistics(statistics.PieCharts...))
return ctx.HTML("Not implemented")
} }

View File

@ -2,13 +2,12 @@ package statistics
import ( import (
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
) )
// Get ... // Get ...
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
statistics := arn.StatisticsCategory{} // statistics := arn.StatisticsCategory{}
arn.DB.GetObject("Cache", "user statistics", &statistics) // arn.DB.GetObject("Cache", "user statistics", &statistics)
return ctx.HTML(components.Statistics(statistics.PieCharts...)) // return ctx.HTML(components.Statistics(statistics.PieCharts...))
return ctx.HTML("Not implemented")
} }

View File

@ -22,14 +22,13 @@ func Get(ctx *aero.Context) string {
} }
// Fetch posts // Fetch posts
postObjects, getErr := arn.DB.GetMany("Post", thread.Posts) postObjects := arn.DB.GetMany("Post", thread.Posts)
posts := make([]*arn.Post, len(postObjects), len(postObjects))
if getErr != nil { for i, obj := range postObjects {
return ctx.Error(http.StatusInternalServerError, "Could not retrieve posts", getErr) posts[i] = obj.(*arn.Post)
} }
posts := postObjects.([]*arn.Post)
// Sort posts // Sort posts
arn.SortPostsLatestLast(posts) arn.SortPostsLatestLast(posts)

View File

@ -10,11 +10,11 @@ import (
// Active ... // Active ...
func Active(ctx *aero.Context) string { func Active(ctx *aero.Context) string {
users, err := arn.GetListOfUsersCached("active users") users := arn.FilterUsers(func(user *arn.User) bool {
return user.IsActive() && user.HasAvatar()
})
if err != nil { arn.SortUsersLastSeen(users)
return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err)
}
return ctx.HTML(components.Users(users)) return ctx.HTML(components.Users(users))
} }

View File

@ -11,7 +11,7 @@ import (
func main() { func main() {
arn.DB.SetScanPriority("high") arn.DB.SetScanPriority("high")
aeroDB := database.New("db", arn.DBTypes) aeroDB := database.New("arn", arn.DBTypes)
defer aeroDB.Close() defer aeroDB.Close()
for typeName := range arn.DB.Types() { for typeName := range arn.DB.Types() {