public ActionResult EditPost(PostUploadViewModel model) { var postService = this.Service <IPostService>(); var postImageService = this.Service <IPostImageService>(); ResponseModel <PostOveralViewModel> response = null; try { Post post = postService.FirstOrDefaultActive(x => x.Id == model.Id); post.ContentType = GetPostType(model); if (model.UploadImage != null) { postImageService.saveImage(post.Id, model.UploadImage); } if (model.DeleteImage != null && model.DeleteImage.Count > 0) { foreach (var delete in model.DeleteImage) { PostImage img = postImageService.FirstOrDefaultActive(x => x.Id == delete); postImageService.Delete(img); } } post.PostContent = model.PostContent; post.EditDate = DateTime.Now; postService.Update(post); PostOveralViewModel result = Mapper.Map <PostOveralViewModel>(post); PreparePostOveralData(result, post.UserId); response = new ResponseModel <PostOveralViewModel>(true, "Bài viết đã được chỉnh sửa!", null, result); } catch (Exception) { response = ResponseModel <PostOveralViewModel> .CreateErrorResponse("Chỉnh sửa thất bại!", systemError); } return(Json(response)); }
public async Task <IActionResult> Upload(PostUploadViewModel model) { ViewBag.SubsectionPages = _subsectionPages; ViewBag.ActiveSubpage = _subsectionPages[2]; if (ModelState.IsValid) { if (!_signInManager.IsSignedIn(User)) { ModelState.AddModelError(string.Empty, "You have to be logged in to upload an art!"); return(Redirect("/posts/upload")); } // Check if the artist exists Artist artist = null; if (model.Author != null) { artist = await _db.Artists .FirstOrDefaultAsync(a => a.ArtistName.Equals(model.Author)); if (artist == null) { // TODO: Ask if u can make a link to the artist registration form in here ModelState.AddModelError(string.Empty, $"Could not find artist named {model.Author}." + " Please consider adding a new artist <a href=\"#\">here</a>"); return(View()); } } // Save the files and manage get the necessary data string large, normal, thumbnail, hash; (int, int)dims; long size; try { using (ImageFileManager ifm = new ImageFileManager("wwwroot/img/posts/", model.File.OpenReadStream(), ImageUtils.ImgExtensionFromContentType(model.File.ContentType))) { large = await ifm.SaveLarge(); normal = await ifm.SaveNormal(); thumbnail = await ifm.SaveThumbnail(0, 0); } hash = ImageUtils.HashFromFile(large); dims = ImageUtils.DimensionsOfImage(large); size = model.File.Length; large = large.Remove(0, 7); normal = normal.Remove(0, 7); thumbnail = thumbnail.Remove(0, 7); } catch (InvalidArtDimensionsException exception) { ModelState.AddModelError(string.Empty, "Invalid art size - the art should be at least 300 x 300px"); return(View()); } // Get the user data and register unregistered tags var usr = await _userManager.GetUserAsync(User); List <string> rawTags = model.TagString.Split(' ').ToList(); List <Tag> tags = new List <Tag>(); foreach (string rawTag in rawTags) { var tag = _db.Tags.FirstOrDefault(t => t.TagString.Equals(rawTag)); if (tag == null) { Tag newTag = new Tag { Id = Guid.NewGuid(), Creator = usr, TagString = rawTag, AddedAt = DateTime.Now }; await _db.Tags.AddAsync(newTag); tags.Add(newTag); } else { tags.Add(tag); } } await _db.SaveChangesAsync(); // Create an art Art art = new Art() { Id = Guid.NewGuid(), Uploader = usr, Name = model.Name, Source = model.Source, Rating = model.Rating, Author = artist, LargeFileUrl = large, FileUrl = normal, PreviewFileUrl = thumbnail, Md5Hash = hash, Height = dims.Item2, Width = dims.Item1, FileSize = (int)size, Stars = 0, CreatedAt = DateTime.Now, }; // Register tag occurrences in the join table and the art List <TagOccurrence> occurrences = new List <TagOccurrence>(); foreach (var tag in tags) { occurrences.Add(new TagOccurrence() { Art = art, ArtId = art.Id, Tag = tag, TagId = tag.Id }); } art.Tags = occurrences; await _db.Arts.AddAsync(art); await _db.SaveChangesAsync(); return(Redirect("/Posts/List")); } return(View()); }
private int GetPostType(PostUploadViewModel model) { var service = this.Service <IPostService>(); int contentType = 0; bool hasText = false; int numberOfImages = 0; if (model.PostContent == null) { hasText = false; } else { hasText = true; } Post post = service.FirstOrDefaultActive(x => x.Id == model.Id); if (post != null) { if (post.PostImages != null) { numberOfImages = post.PostImages.Count(); } } if (model.UploadImage != null && model.UploadImage.Count() > 0) { numberOfImages = numberOfImages + model.UploadImage.Count(); } if (model.DeleteImage != null && model.DeleteImage.Count() > 0) { numberOfImages = numberOfImages - model.DeleteImage.Count(); } if (numberOfImages == 0 && hasText) { contentType = int.Parse(ContentPostType.TextOnly.ToString("d")); } else if (numberOfImages == 1 && hasText) { contentType = int.Parse(ContentPostType.TextAndImage.ToString("d")); } else if (numberOfImages > 1 && hasText) { contentType = int.Parse(ContentPostType.TextAndMultiImages.ToString("d")); } else if (numberOfImages > 1 && !hasText) { contentType = int.Parse(ContentPostType.MultiImages.ToString("d")); } else if (numberOfImages == 1 && !hasText) { contentType = int.Parse(ContentPostType.ImageOnly.ToString("d")); } return(contentType); }
public ActionResult PostOnTimeLine(PostUploadViewModel model, string profileId) { var service = this.Service <IPostService>(); var aspNetUserService = this.Service <IAspNetUserService>(); var notiService = this.Service <INotificationService>(); var memberService = this.Service <IGroupMemberService>(); PostOveralViewModel result = null; ResponseModel <PostOveralViewModel> response = null; try { Post post = Mapper.Map <Post>(model); post.ProfileId = profileId; if (model.PostContent == null) { post.PostContent = ""; } post.ContentType = GetPostType(model); if (post.ContentType != int.Parse(ContentPostType.TextOnly.ToString("d"))) { FileUploader uploader = new FileUploader(); foreach (var img in model.UploadImage) { PostImage image = new PostImage(); image.Image = uploader.UploadImage(img, userImagePath); post.PostImages.Add(image); } } post = service.CreatePost(post); ////Notify all group members //if (post.GroupId != null) //{ // List<GroupMember> memberList = GetMemberList(post.GroupId); // AspNetUser postedUser = aspNetUserService.FindUser(post.UserId); // foreach (var member in memberList) // { // if (!(member.UserId.Equals(post.UserId))) // { // Notification noti = notiService.SaveNoti(member.UserId, post.UserId, "Post", postedUser.FullName + " đã đăng một bài viết", (int)NotificationType.Post, post.Id, null, null); // List<string> registrationIds = GetToken(member.UserId); // if (registrationIds != null && registrationIds.Count != 0) // { // NotificationModel notiModel = Mapper.Map<NotificationModel>(PrepareNotificationViewModel(noti)); // Android.Notify(registrationIds, null, notiModel); // } // } // } //} if (post.UserId != profileId) { var _notificationService = this.Service <INotificationService>(); var _userService = this.Service <IAspNetUserService>(); AspNetUser sender = _userService.FindUser(post.UserId); string title = Utils.GetEnumDescription(NotificationType.Post); int type = (int)NotificationType.Post; string message = sender.FullName + " đã đăng một bài viết lên tường nhà bạn"; Notification noti = _notificationService.CreateNoti(profileId, post.UserId, title, message, type, post.Id, null, null, null); ////////////////////////////////////////////// //signalR noti NotificationFullInfoViewModel notiModel = _notificationService.PrepareNoti(Mapper.Map <NotificationFullInfoViewModel>(noti)); // Get the context for the Pusher hub IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext <RealTimeHub>(); // Notify clients in the group hubContext.Clients.User(notiModel.UserId).send(notiModel); } //Missing post sport result = Mapper.Map <PostOveralViewModel>(post); result.AspNetUser = Mapper.Map <AspNetUserSimpleModel>(aspNetUserService.FindUser(result.UserId)); PreparePostOveralData(result, post.UserId); response = new ResponseModel <PostOveralViewModel>(true, "Đăng bài thành công!", null, result); } catch (Exception) { response = ResponseModel <PostOveralViewModel> .CreateErrorResponse("Đăng bài thất bại!", systemError); } return(Json(response)); }