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 users
import (
"errors"
"net/http"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/log"
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
"github.com/NyaaPantsu/nyaa/models"
2017-07-02 16:54:55 +02:00
"github.com/NyaaPantsu/nyaa/utils/validator"
2017-07-02 23:53:23 +02:00
"github.com/NyaaPantsu/nyaa/utils/validator/user"
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
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
)
// UpdateFromRequest updates a user.
2017-07-03 02:31:17 +02:00
func UpdateFromRequest ( c * gin . Context , form * userValidator . UserForm , formSet * userValidator . UserSettingsForm , currentUser * models . User , id uint ) ( * models . User , int , error ) {
var user = & models . User { }
if models . ORM . First ( user , id ) . RecordNotFound ( ) {
return user , http . StatusNotFound , errors . New ( "user_not_found" )
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-07-03 02:31:17 +02:00
if ! currentUser . IsModerator ( ) { // We don't want users to be able to modify some fields
form . Status = user . Status
form . Username = user . Username
}
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
if form . Password != "" {
2017-07-02 23:53:23 +02:00
errBcrypt := bcrypt . CompareHashAndPassword ( [ ] byte ( user . Password ) , [ ] byte ( form . CurrentPassword ) )
if errBcrypt != nil && ! currentUser . IsModerator ( ) {
2017-07-03 02:31:17 +02:00
return user , http . StatusInternalServerError , errors . New ( "incorrect_password" )
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-07-02 23:53:23 +02:00
newPassword , errBcrypt := bcrypt . GenerateFromPassword ( [ ] byte ( form . Password ) , 10 )
if errBcrypt != nil {
2017-07-03 02:31:17 +02:00
return user , http . StatusInternalServerError , errors . New ( "error_password_generating" )
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
}
form . Password = string ( newPassword )
} else { // Then no change of password
form . Password = user . Password
}
log . Debugf ( "form %+v\n" , form )
2017-07-03 02:31:17 +02:00
validator . Bind ( user , form )
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
// We set settings here
user . ParseSettings ( )
user . Settings . Set ( "new_torrent" , formSet . NewTorrent )
user . Settings . Set ( "new_torrent_email" , formSet . NewTorrentEmail )
user . Settings . Set ( "new_comment" , formSet . NewComment )
user . Settings . Set ( "new_comment_email" , formSet . NewCommentEmail )
user . Settings . Set ( "new_responses" , formSet . NewResponses )
user . Settings . Set ( "new_responses_email" , formSet . NewResponsesEmail )
user . Settings . Set ( "new_follower" , formSet . NewFollower )
user . Settings . Set ( "new_follower_email" , formSet . NewFollowerEmail )
user . Settings . Set ( "followed" , formSet . Followed )
user . Settings . Set ( "followed_email" , formSet . FollowedEmail )
user . SaveSettings ( )
2017-07-03 02:31:17 +02:00
status , err := user . Update ( )
return user , status , err
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
}