Fix CreatedBy and UpdatedBy

This commit is contained in:
Andrea Fazzi 2019-12-09 18:07:43 +01:00
parent a9437536d0
commit 23fb973590
3 changed files with 36 additions and 19 deletions

View file

@ -2,7 +2,6 @@ package orm
import (
"fmt"
"log"
"strconv"
"net/http"

View file

@ -2,7 +2,6 @@ package orm
import (
"fmt"
"log"
"net/http"
"strings"
"time"
@ -252,7 +251,7 @@ func SaveSchool(school interface{}) (interface{}, error) {
func (model *School) HasCategory(participant *Participant) (bool, error) {
var participants []*Participant
log.Println(model)
if err := DB().Where("category_id = ? AND school_id = ? AND id <> ?", participant.CategoryID, model.ID, participant.ID).Find(&participants).Error; err != nil {
return false, err
}

View file

@ -1,6 +1,7 @@
package orm
import (
"log"
"net/http"
"github.com/dgrijalva/jwt-go"
@ -40,16 +41,25 @@ func NewUserModifierCreate(r *http.Request) *UserModifierCreate {
}
func (um *UserModifierCreate) CreatedBy() (*UserAction, error) {
action := new(UserAction)
switch {
case (um.CreatorRole == "participant") || (um.CreatorRole == "school"):
if err := DB().First(&action.User, um.CreatorID).Error; err != nil {
switch um.CreatorRole {
case "participant":
var participant Participant
if err := DB().Preload("User").First(&participant, um.CreatorID).Error; err != nil {
return nil, err
}
case um.CreatorRole == "subscriber":
action.User = *participant.User
case "school":
var school School
if err := DB().Preload("User").First(&school, um.CreatorID).Error; err != nil {
return nil, err
}
action.User = *school.User
case "subscriber":
action.User = SubscriberUser
case um.CreatorRole == "administrator":
case "administrator":
action.User = AdministratorUser
default:
return nil, nil
@ -64,7 +74,7 @@ func NewUserModifierUpdate(r *http.Request) *UserModifierUpdate {
if r.Context().Value("user") != nil {
claims = r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
}
log.Println("UPDATE Inside useraction", claims["user_id"])
return &UserModifierUpdate{
UpdaterID: claims["user_id"].(string),
UpdaterRole: claims["role"].(string),
@ -72,21 +82,30 @@ func NewUserModifierUpdate(r *http.Request) *UserModifierUpdate {
}
}
func (um *UserModifierUpdate) UpdatedBy() (*User, error) {
var user User
func (um *UserModifierUpdate) UpdatedBy() (*UserAction, error) {
action := new(UserAction)
switch {
case (um.UpdaterRole == "participant") || (um.UpdaterRole == "school"):
if err := DB().First(&user, um.UpdaterID).Error; err != nil {
switch um.UpdaterRole {
case "participant":
var participant Participant
if err := DB().Preload("User").First(&participant, um.UpdaterID).Error; err != nil {
return nil, err
}
case um.UpdaterRole == "subscriber":
user = SubscriberUser
case um.UpdaterRole == "administrator":
user = AdministratorUser
action.User = *participant.User
case "school":
var school School
if err := DB().Preload("User").First(&school, um.UpdaterID).Error; err != nil {
return nil, err
}
action.User = *school.User
case "subscriber":
action.User = SubscriberUser
case "administrator":
action.User = AdministratorUser
default:
return nil, nil
}
return &user, nil
return action, nil
}