This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
package notifications
import (
"github.com/NyaaPantsu/nyaa/models"
)
// NotifyUser : Notify a user with a notification according to his settings
func NotifyUser ( user * models . User , name string , msg string , url string , email bool ) {
if user . ID > 0 {
notification := models . NewNotification ( name , msg , url )
notification . UserID = user . ID
models . ORM . Create ( & notification )
// TODO: Email notification
/ * if email {
} * /
}
}
// ToggleReadNotification : Make a notification as read according to its identifier
2017-10-29 19:28:25 +01:00
func ToggleReadNotification ( identifier string , user * models . User ) { //
models . ORM . Model ( & models . Notification { } ) . Where ( "identifier = ? AND user_id = ?" , identifier , user . ID ) . Updates ( models . Notification { Read : true } )
for i , notif := range user . Notifications {
if notif . Identifier == identifier {
user . Notifications [ i ] . Read = true
}
}
//Need to update both DB and variable, otherwise when the function is called the user still needs to do an additional refresh to see the notification gone/read
}
// MarkAllNotificationsAsRead : Force every notification as read
func MarkAllNotificationsAsRead ( user * models . User ) { //
models . ORM . Model ( & models . Notification { } ) . Where ( "user_id = ?" , user . ID ) . Updates ( models . Notification { Read : true } )
for i := range user . Notifications {
user . Notifications [ i ] . Read = true
}
//Need to update both DB and variable, otherwise when the function is called the user still needs to do an additional refresh to see the notification gone/read
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
}
2017-10-29 00:17:48 +02:00
// DeleteNotifications : Erase notifications from a user
2017-10-29 19:28:25 +01:00
func DeleteNotifications ( user * models . User , all bool ) { //
2017-10-29 00:17:48 +02:00
if all {
2017-10-29 19:28:25 +01:00
models . ORM . Where ( "user_id = ?" , user . ID ) . Delete ( & models . Notification { } )
user . Notifications = [ ] models . Notification { }
2017-10-29 00:17:48 +02:00
} else {
2017-10-29 19:28:25 +01:00
models . ORM . Where ( "user_id = ? AND read = ?" , user . ID , true ) . Delete ( & models . Notification { } )
NewNotifications := [ ] models . Notification { }
for _ , notif := range user . Notifications {
if ! notif . Read {
NewNotifications = append ( NewNotifications , notif )
}
}
user . Notifications = NewNotifications
2017-10-29 00:17:48 +02:00
}
This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
}