public async Task <HttpResponseMessage> EditUserMobile() { try { var db = Global.DB; var root = HttpContext.Current.Server.MapPath("~/Media/"); var startingPosition = root.Length - 6; var editUserDto = new MobileUserDto(); var provider = new CustomMultipartFormDataStreamProvider(root); // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } StringBuilder sb = new StringBuilder(); // Holds the response body // Read the form data and return an async task. await Request.Content.ReadAsMultipartAsync(provider); // This illustrates how to get the form data. foreach (var key in provider.FormData.AllKeys) { foreach (var value in provider.FormData.GetValues(key)) { if (key.Equals("accessKey")) { if (!Global.CheckAccessKey(value)) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Global.Message_WrongAccessKey)); } } foreach (PropertyInfo propertyInfo in editUserDto.GetType().GetProperties()) { if (key.Equals(propertyInfo.Name)) { var propType = editUserDto.GetType().GetProperty(propertyInfo.Name).PropertyType; var converter = TypeDescriptor.GetConverter(propType); var convertedObject = converter.ConvertFromString(value); editUserDto.GetType().GetProperty(propertyInfo.Name).SetValue(editUserDto, convertedObject); } } } } // This illustrates how to get the file names for uploaded files. foreach (var file in provider.FileData) { var splitted = file.LocalFileName.Split('\\'); root += "UserPicture\\" + splitted[splitted.Length - 1]; try { if (File.Exists(root)) { File.Delete(root); } File.Move(file.LocalFileName, root); } catch (DirectoryNotFoundException) { new FileInfo(root).Directory.Create(); File.Move(file.LocalFileName, root); } FileInfo fileInfo = new FileInfo(file.LocalFileName); sb.Append(string.Format("{0}", root)); } var targetUser = db.Users.SingleOrDefault(x => x.Id == editUserDto.Id); targetUser.Name = editUserDto.Name; targetUser.PhoneNumber = editUserDto.PhoneNumber; targetUser.Address = editUserDto.Address; targetUser.Description = editUserDto.Description; targetUser.PIC = editUserDto.PIC; targetUser.Show = editUserDto.Show; targetUser.KeyFeatures = editUserDto.KeyFeatures; targetUser.CoverageArea = editUserDto.CoverageArea; targetUser.YearsOfExperience = editUserDto.YearsOfExperience; targetUser.Availability = editUserDto.Availability; targetUser.Styling = editUserDto.Styling; targetUser.Clipping = editUserDto.Styling; targetUser.TrainingYears = editUserDto.TrainingYears; targetUser.TrainingCourses = editUserDto.TrainingCourses; try { targetUser.TrainingStartDate = Global.ParseStringToDate(editUserDto.TrainingStartDate); } catch (FormatException) { } catch (ArgumentNullException) { } if (provider.FileData.Count() > 0) { targetUser.Picture = Global.GetServerPathFromAUploadPath(sb.ToString(), 3); } db.SaveChanges(); var um = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var roleUser = um.GetRoles(targetUser.Id).FirstOrDefault(); var User = Mapper.Map <ApplicationUser, MobileUserViewModel>(targetUser); User.Role = roleUser; return(Request.CreateResponse(HttpStatusCode.OK, new { User }, MediaTypeHeaderValue.Parse("application/json"))); } catch (NullReferenceException) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Global.Message_ErrorMessage)); } catch (ArgumentOutOfRangeException) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Global.Message_ErrorMessage)); } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, Global.Message_ErrorMessage)); } }
public async Task <HttpResponseMessage> RegisterUserMobile() { try { var db = Global.DB; var root = HttpContext.Current.Server.MapPath("~/Media/"); var startingPosition = root.Length - 6; var newUserDto = new MobileUserDto(); var um = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var passwordHasher = new PasswordHasher(); var provider = new CustomMultipartFormDataStreamProvider(root); var roleStore = new RoleStore <IdentityRole>(db); var roleManager = new RoleManager <IdentityRole>(roleStore); // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } StringBuilder sb = new StringBuilder(); // Holds the response body // Read the form data and return an async task. await Request.Content.ReadAsMultipartAsync(provider); // This illustrates how to get the form data. foreach (var key in provider.FormData.AllKeys) { foreach (var value in provider.FormData.GetValues(key)) { if (key.Equals("accessKey")) { if (!Global.CheckAccessKey(value)) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Global.Message_WrongAccessKey)); } } foreach (PropertyInfo propertyInfo in newUserDto.GetType().GetProperties()) { if (key.Equals(propertyInfo.Name)) { var propType = newUserDto.GetType().GetProperty(propertyInfo.Name).PropertyType; var converter = TypeDescriptor.GetConverter(propType); var convertedObject = converter.ConvertFromString(value); newUserDto.GetType().GetProperty(propertyInfo.Name).SetValue(newUserDto, convertedObject); } } } } // This illustrates how to get the file names for uploaded files. foreach (var file in provider.FileData) { var splitted = file.LocalFileName.Split('\\'); root += "UserPicture\\" + splitted[splitted.Length - 1]; try { if (File.Exists(root)) { File.Delete(root); } File.Move(file.LocalFileName, root); } catch (DirectoryNotFoundException) { new FileInfo(root).Directory.Create(); File.Move(file.LocalFileName, root); } FileInfo fileInfo = new FileInfo(file.LocalFileName); sb.Append(string.Format("{0}", root)); } var pathUrl = provider.FileData.Count() == 0 ? null : Global.GetServerPathFromAUploadPath(sb.ToString(), 3); var user = new ApplicationUser { CreatedDate = DateTime.Now, PasswordHash = passwordHasher.HashPassword(newUserDto.Password), Name = newUserDto.Name, UserName = newUserDto.Email, Email = newUserDto.Email, PhoneNumber = newUserDto.PhoneNumber, Address = newUserDto.Address, Description = newUserDto.Description, Picture = pathUrl, PIC = newUserDto.PIC, KeyFeatures = newUserDto.KeyFeatures, CoverageArea = newUserDto.CoverageArea, YearsOfExperience = newUserDto.YearsOfExperience, Availability = newUserDto.Availability, Styling = newUserDto.Styling, Clipping = newUserDto.Styling, TrainingYears = newUserDto.TrainingYears, TrainingCourses = newUserDto.TrainingCourses }; try { user.TrainingStartDate = Global.ParseStringToDate(newUserDto.TrainingStartDate); } catch (FormatException) { } catch (ArgumentNullException) { } db.Users.Add(user); if (!roleManager.RoleExists(newUserDto.Role)) { roleManager.Create(new IdentityRole(newUserDto.Role)); } IdentityUserRole userRole = new IdentityUserRole(); userRole.UserId = user.Id; userRole.RoleId = roleManager.FindByName(newUserDto.Role).Id; db.UserRoles.Add(userRole); db.SaveChanges(); var getUser = db.Users.SingleOrDefault(item => item.Email == newUserDto.Email); var roleUser = um.GetRoles(getUser.Id).FirstOrDefault(); var User = Mapper.Map <ApplicationUser, MobileUserViewModel>(getUser); User.Role = roleUser; return(Request.CreateResponse(HttpStatusCode.OK, new { User }, MediaTypeHeaderValue.Parse("application/json"))); } catch (NullReferenceException) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Global.Message_ErrorMessage)); } catch (ArgumentOutOfRangeException) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Global.Message_ErrorMessage)); } catch (DbEntityValidationException) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request has invalid data!")); } }