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 {
// Add FacebookToUser reference
err = user.ConnectFacebook(fbUser.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not connect account to Facebook account", err)
}
user.ConnectFacebook(fbUser.ID)
// Save in DB
user.Save()
@ -110,7 +106,7 @@ func InstallFacebookAuth(app *aero.Application) {
var getErr error
// 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 {
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()
// Register user
err = arn.RegisterUser(user)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not register a new user", err)
}
arn.RegisterUser(user)
// Connect account to a Facebook account
err = user.ConnectFacebook(fbUser.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not connect account to Facebook account", err)
}
user.ConnectFacebook(fbUser.ID)
// Save user object again with updated data
user.Save()

View File

@ -102,11 +102,7 @@ func InstallGoogleAuth(app *aero.Application) {
if user != nil {
// Add GoogleToUser reference
err = user.ConnectGoogle(googleUser.Sub)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not connect account to Google account", err)
}
user.ConnectGoogle(googleUser.Sub)
// Save in DB
user.Save()
@ -120,7 +116,7 @@ func InstallGoogleAuth(app *aero.Application) {
var getErr error
// 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 {
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()
// Register user
err = arn.RegisterUser(user)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not register a new user", err)
}
arn.RegisterUser(user)
// Connect account to a Google account
err = user.ConnectGoogle(googleUser.Sub)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not connect account to Google account", err)
}
user.ConnectGoogle(googleUser.Sub)
// Save user object again with updated data
user.Save()

View File

@ -2,7 +2,6 @@ package main
import (
"github.com/aerogo/aero"
"github.com/aerogo/session-store-aerospike"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/auth"
"github.com/animenotifier/notify.moe/components/css"
@ -69,7 +68,10 @@ func configure(app *aero.Application) *aero.Application {
// Sessions
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
app.Layout = layout.Render
@ -210,8 +212,6 @@ func configure(app *aero.Application) *aero.Application {
// Domain
if arn.IsDevelopment() {
app.Config.Domain = "beta.notify.moe"
} else {
arn.DB.SetScanPriority("high")
}
// Authentication

View File

@ -48,11 +48,7 @@ func Select(ctx *aero.Context) string {
Results: []interface{}{},
}
stream, err := arn.DB.All(dataTypeName)
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error fetching data from the database", err)
}
stream := arn.DB.All(dataTypeName)
process := func(obj interface{}) {
_, _, value, _ := mirror.GetField(obj, field)
@ -62,47 +58,8 @@ func Select(ctx *aero.Context) string {
}
}
switch dataTypeName {
case "Analytics":
for obj := range stream.(chan *arn.Analytics) {
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 stream {
process(obj)
}
for _, obj := range response.Results {

View File

@ -1,7 +1,6 @@
package editor
import (
"net/http"
"sort"
"github.com/aerogo/aero"
@ -13,14 +12,10 @@ const maxAniListEntries = 70
// AniList ...
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") == ""
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "Couldn't filter anime", err)
}
sort.Slice(missing, func(i, j int) bool {
a := missing[i]
b := missing[j]

View File

@ -1,7 +1,6 @@
package editor
import (
"net/http"
"sort"
"github.com/aerogo/aero"
@ -13,14 +12,10 @@ const maxShoboiEntries = 70
// Shoboi ...
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") == ""
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "Couldn't filter anime", err)
}
sort.Slice(missing, func(i, j int) bool {
a := missing[i]
b := missing[j]

View File

@ -2,20 +2,19 @@ package explore
import (
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
)
// Get ...
func Get(ctx *aero.Context) string {
var cache arn.ListOfIDs
err := arn.DB.GetObject("Cache", "airing anime", &cache)
// var cache arn.ListOfIDs
// err := arn.DB.GetObject("Cache", "airing anime", &cache)
airing, err := arn.GetAiringAnimeCached()
// airing, err := arn.GetAiringAnimeCached()
if err != nil {
return ctx.Error(500, "Couldn't fetch airing anime", err)
}
// if err != nil {
// 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.
func Get(ctx *aero.Context) string {
tag := ctx.Get("tag")
threads, _ := arn.GetThreadsByTag(tag)
threads := arn.GetThreadsByTag(tag)
arn.SortThreads(threads)
if len(threads) > ThreadsPerPage {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,13 +2,12 @@ package statistics
import (
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
)
// Get ...
func Get(ctx *aero.Context) string {
statistics := arn.StatisticsCategory{}
arn.DB.GetObject("Cache", "user statistics", &statistics)
return ctx.HTML(components.Statistics(statistics.PieCharts...))
// statistics := arn.StatisticsCategory{}
// arn.DB.GetObject("Cache", "user statistics", &statistics)
// 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
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 {
return ctx.Error(http.StatusInternalServerError, "Could not retrieve posts", getErr)
for i, obj := range postObjects {
posts[i] = obj.(*arn.Post)
}
posts := postObjects.([]*arn.Post)
// Sort posts
arn.SortPostsLatestLast(posts)

View File

@ -10,11 +10,11 @@ import (
// Active ...
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 {
return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err)
}
arn.SortUsersLastSeen(users)
return ctx.HTML(components.Users(users))
}

View File

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