Ejemplo n.º 1
0
        public HttpResponseMessage SaveUser(UserModel user)
        {
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            if (AccountBLL.UserNameExists(user.Username, user.Id))
            {
                response.StatusCode = HttpStatusCode.BadRequest;
                response.Content    = new ObjectContent <string>
                                          ("Användarnamnet finns redan, vänligen välj ett annat.", new JsonMediaTypeFormatter());
                return(response);
            }
            Account acc     = UserModel.ConvertToAccount(user);
            Account account = AccountBLL.SaveAccount(acc);
            List <Account_Information_Generic_Value> genericValues =
                AccountInformationGeneric.MapGenericValues(user.GenericValues, account.ID);

            AccountBLL.SaveGenericValues(genericValues, account.ClubId);
            //response.Content = new ObjectContent<Account>(account, new JsonMediaTypeFormatter());
            return(response);
        }
Ejemplo n.º 2
0
        public static UserModel MapUserModel(Account account, bool deepLoad, bool loadUserInformation = false)
        {
            if (account != null)
            {
                UserModel userModel = new UserModel()
                {
                    Id        = account.ID,
                    FirstName = account.FirstName,
                    LastName  = account.LastName,
                    Username  = account.UserName,
                    Image     = account.Image,
                    ClubId    = account.ClubId,
                    Gender    = (Gender)account.Gender
                };

                userModel.Club = new ClubModel()
                {
                    Id        = account.ClubId,
                    Name      = account.Club.Name,
                    ShortName = account.Club.ShortName,
                    Image     = account.Club.Image
                };

                if (deepLoad)
                {
                    Parallel.Invoke(() =>
                    {
                        userModel.Token = AccountBLL.GetUserSession(account.ID);
                    }, () =>
                    {
                        foreach (var item in account.AccountAccess)
                        {
                            foreach (var right in item.Accessright.Accessright_Right)
                            {
                                userModel.AccessRightsRight.Add(new AccessrightRightModel()
                                {
                                    AccessType      = (AccessType)right.AccessType,
                                    AccessTypeRight = (AccessTypeRight)right.AccessTypeRight,
                                    Id = right.Id
                                });
                            }
                        }
                    }, () =>
                    {
                        foreach (var item in account.AccountAccess)
                        {
                            userModel.AccessRights.Add(new AccessrightModel()
                            {
                                Id          = item.Accessright.ID,
                                Name        = item.Accessright.Name,
                                Description = item.Accessright.Description
                            });
                        }
                    }, () =>
                    {
                        Account_Information information = AccountBLL.GetAccountSettings(userModel.Id);
                        if (information != null)
                        {
                            userModel.UserInformation = new UserInformationModel()
                            {
                                Email      = information.Email,
                                City       = information.City,
                                Occupation = information.Occupation,
                                Phone      = information.Phone,
                                Street     = information.Street,
                                Zip        = information.Zip,
                                Birthday   = information.Birthday,
                                Grade      = (SW.Core.Enums.Grade)information.Grade,
                                Weight     = information.Weight,
                                Theme      = information.Theme
                            };
                        }
                    }, () =>
                    {
                        userModel.AccountAccess = AccountAccessModel.MapAccountAccesses(AccountBLL.GetAccountAccesses(userModel.Id));
                    }, () =>
                    {
                        userModel.GenericValues = AccountInformationGeneric.MapValues(
                            AccountBLL.GetGenericValues(userModel.Id, userModel.ClubId));
                    });
                }
                else if (loadUserInformation)
                {
                    Account_Information information = AccountBLL.GetAccountSettings(userModel.Id);
                    if (information != null)
                    {
                        userModel.UserInformation = new UserInformationModel()
                        {
                            Email      = information.Email,
                            City       = information.City,
                            Occupation = information.Occupation,
                            Phone      = information.Phone,
                            Street     = information.Street,
                            Zip        = information.Zip,
                            Birthday   = information.Birthday,
                            Grade      = (SW.Core.Enums.Grade)information.Grade,
                            Weight     = information.Weight,
                            Theme      = information.Theme
                        };
                    }
                }

                return(userModel);
            }

            return(new UserModel()
            {
                GenericValues = AccountInformationGeneric.MapValues(
                    AccountBLL.GetGenericValues(0, 0))
            });
        }