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; }
public HttpResponseMessage SaveMe(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; } UserPrincipal loggedInUser = (UserPrincipal)HttpContext.Current.User; user.Id = loggedInUser.AccountSession.AccountId; user.ClubId = loggedInUser.AccountSession.ClubId; Account acc = UserModel.ConvertToAccount(user); Account account = AccountBLL.SaveAccount(acc); //response.Content = new ObjectContent<Account>(account, new JsonMediaTypeFormatter()); return response; }
internal static Account ConvertToAccount(UserModel user) { Account acc = new Account() { ID = user.Id, FirstName = user.FirstName, LastName = user.LastName, Password = user.Password, UserName = user.Username, Image = user.Image, ClubId = user.Club.Id, Gender = (int)user.Gender }; acc.Account_Information.Add(new Account_Information() { Email = user.UserInformation.Email, City = user.UserInformation.City, Occupation = user.UserInformation.Occupation, Phone = user.UserInformation.Phone, Street = user.UserInformation.Street, Zip = user.UserInformation.Zip, Grade = (int)user.UserInformation.Grade, Birthday = user.UserInformation.Birthday, Weight = user.UserInformation.Weight, Theme = user.UserInformation.Theme }); foreach (var access in user.AccountAccess) { acc.AccountAccess.Add(new AccountAccess() { ID = access.Id, AccessID = access.AccessId, AccountID = user.Id }); } return acc; }
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 = (Gradera.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 = (Gradera.Core.Enums.Grade)information.Grade, Weight = information.Weight, Theme = information.Theme }; } } return userModel; } return new UserModel() { GenericValues = AccountInformationGeneric.MapValues( AccountBLL.GetGenericValues(0, 0)) }; }