public ActionResult Create(CommunityAddViewModel model, HttpPostedFileBase file) { if (file != null) { var isImage = FileUtils.IsImage(file); if (!isImage) { this.ModelState.AddModelError("ProfileImage", "The file should be an image"); } var avatar = this.images.GetProfileImage <CommunityImage>(file, ImageType.Community); bool exists = Directory.Exists(this.Server.MapPath(avatar.UrlPath)); if (!exists) { Directory.CreateDirectory(this.Server.MapPath(avatar.UrlPath)); } file.SaveAs(this.Server.MapPath(Path.Combine(avatar.UrlPath, avatar.NewFileName))); model.CommunityImageId = ((CommunityImage)avatar).Id; } else { model.CommunityImageId = ((CommunityImage)this.images.GetDefualtCoverImage()).Id; } var userId = this.User.Identity.GetUserId(); var mapped = this.Mapper.Map <Community>(model); if (this.ModelState.IsValid) { this.communities.Add(mapped, model.City, model.District, userId); this.users.AddToRole(userId, "Administrator"); return(this.RedirectToAction("All", "Communities")); } return(this.View()); }
public ActionResult Create(CommunityAddViewModel model, HttpPostedFileBase file) { if (file != null) { var isImage = FileUtils.IsImage(file); if (!isImage) { this.ModelState.AddModelError("ProfileImage", "The file should be an image"); } var avatar = this.images.GetProfileImage<CommunityImage>(file, ImageType.Community); bool exists = Directory.Exists(this.Server.MapPath(avatar.UrlPath)); if (!exists) { Directory.CreateDirectory(this.Server.MapPath(avatar.UrlPath)); } file.SaveAs(this.Server.MapPath(Path.Combine(avatar.UrlPath, avatar.NewFileName))); model.CommunityImageId = ((CommunityImage)avatar).Id; } else { model.CommunityImageId = ((CommunityImage)this.images.GetDefualtCoverImage()).Id; } var userId = this.User.Identity.GetUserId(); var mapped = this.Mapper.Map<Community>(model); if (this.ModelState.IsValid) { this.communities.Add(mapped, model.City, model.District, userId); this.users.AddToRole(userId, "Administrator"); return this.RedirectToAction("All", "Communities"); } return this.View(); }