public HttpResponseMessage AddCommentToSong([FromUri] long id, [FromBody] CommentSongModel commentModel) { if (commentModel.DataId != id) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Dữ liệu không phù hợp")); } using (var db = new OnlineMusicEntities()) { SongComment comment = new SongComment(); commentModel.UpdateEntity(comment); comment.Date = DateTime.Now; db.SongComments.Add(comment); db.SaveChanges(); comment.User = (from u in db.Users where u.Id == comment.UserId select u).FirstOrDefault(); commentModel = commentDto.GetCommentQuery(db, swhereClause: null).Where(c => c.Id == comment.Id).FirstOrDefault(); try { Song song = (from s in db.Songs where s.Id == id select s).FirstOrDefault(); if (song != null && song.AuthorId != comment.UserId) { string action = NotificationAction.COMMENT_AUDIO + "_" + song.Id; Notification notification = (from ntf in db.Notifications where ntf.UserId == song.AuthorId && ntf.Action == action select ntf).FirstOrDefault(); if (notification == null) { notification = new Notification() { Title = "Hệ thống", IsMark = false, Action = action, UserId = song.AuthorId }; db.Notifications.Add(notification); } UserInfoModel info = commentModel.UserInfo; string actor = info != null && !String.IsNullOrEmpty(info.FullName) ? info.FullName : comment.User?.Username; long commentCount = song.SongComments.Select(c => c.UserId).Distinct().Count(); if (commentCount > 1) { actor += " và " + (commentCount - 1) + " người khác"; } notification.Message = $"{actor} đã comment vào" + (song.Resource.Type == (int)ResourceTypeManager.Audio ? " bài hát " : " video ") + song.Title + " của bạn"; notification.CreatedAt = DateTime.Now; db.SaveChanges(); } } catch { } return(Request.CreateResponse(HttpStatusCode.Created, commentModel)); } }
public void saveComment(SongComment commentToSave) { var song = _repo.Query <Song>().Where(s => s.Id == commentToSave.SongId).FirstOrDefault(); if (song != null) { var commentToCreate = new SongComment { Message = commentToSave.Message, SongId = commentToSave.SongId }; _repo.Add <SongComment>(commentToCreate); } }
public CommentSongModel Converter(SongComment c) { return(new CommentSongModel() { Id = c.Id, Comment = c.Comment, UserId = c.UserId, Date = c.Date, DataId = c.SongId, UserInfo = userInfoDto.ConvertToUserInfoModel(c.User.UserInfoes).FirstOrDefault() }); }
public string AddSongComment([FromBody] object json) { JObject j = JObject.Parse(json.ToString()); //获取评论内容 string sid = j["sid"].ToString(); string pid = j["pid"].ToString(); string content = j["content"].ToString(); string from = j["from"].ToString(); string to = j["to"].ToString(); SongComment c = new SongComment(); if (sid != "") { c.SC_Song = int.Parse(sid); } else { return("参数不完整"); } if (pid != "") { c.SC_Pid = int.Parse(pid); } if (content != "") { c.SC_Content = content; } else { return("参数不完整"); } if (from != "") { c.SC_From_User = int.Parse(from); } else { return("参数不完整"); } if (to != "") { c.SC_To_User = int.Parse(to); } else { c.SC_To_User = int.Parse(from); } c.SC_UpTime = DateTime.Now; return(SongCommentService.AddSongComment(c)); }
/// <summary> /// 发布评论 /// </summary> /// <param name="c"></param> /// <returns></returns> internal static string AddSongComment(SongComment c) { using (YoungMusicEntities db = new YoungMusicEntities()) { db.SongComment.Add(c); if (db.SaveChanges() > 0) { return("发布成功"); } else { return("发布失败"); } } }
public HttpResponseMessage DeleteComment([FromUri] long songId, [FromUri] long commentId) { using (var db = new OnlineMusicEntities()) { SongComment comment = (from c in db.SongComments where c.Id == commentId select c).FirstOrDefault(); if (comment == null) { return(Request.CreateErrorResponse(HttpStatusCode.OK, "Không tìm thấy comment id=" + commentId)); } db.SongComments.Remove(comment); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } }
public ActionResult Create(int songId, string content) { object result; if (CurrentUser == null) { result = new { type = "ERROR", msg = "Bạn cần đăng nhập để đăng bình luận!" }; } SongComment songComment = new SongComment(); songComment.UserId = CurrentUser.UserId; songComment.SongId = songId; songComment.Content = content; songComment.PostTime = DateTime.Now; if (ModelState.IsValid) { db.SongComment.Add(songComment); db.SaveChanges(); ViewEngineResult view = ViewEngines.Engines.FindPartialView(ControllerContext, "~/Views/Song/_CommentDisplay.cshtml"); using (StringWriter writer = new StringWriter()) { db.Entry(songComment).Reference(s => s.User).Load(); db.Entry(songComment).Reference(s => s.Song).Load(); this.ViewData.Model = songComment; var context = new ViewContext(ControllerContext, view.View, ViewData, TempData, writer); view.View.Render(context, writer); writer.Flush(); content = writer.ToString(); } result = new { type = "SUCCESS", html = content }; } else { result = new { type = "ERROR", msg = "Có lỗi xảy ra!" } }; JsonResult jsonResult = new JsonResult(); jsonResult.Data = result; jsonResult.ContentEncoding = System.Text.Encoding.UTF8; return(jsonResult); }
public HttpResponseMessage EditComment([FromUri] long id, [FromBody] CommentSongModel commentModel) { if (commentModel.DataId != id) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Dữ liệu không phù hợp")); } using (var db = new OnlineMusicEntities()) { SongComment comment = (from c in db.SongComments where c.Id == commentModel.Id select c).FirstOrDefault(); if (comment == null) { return(Request.CreateErrorResponse(HttpStatusCode.OK, "Không tìm thấy comment id=" + commentModel.Id)); } commentModel.UpdateEntity(comment); db.SaveChanges(); commentModel = commentDto.GetCommentQuery(db, (SongComment c) => c.Id == comment.Id).FirstOrDefault(); return(Request.CreateResponse(HttpStatusCode.OK, commentModel)); } }
public IActionResult Post([FromBody] SongComment value) { _repo.saveComment(value); return(Ok()); }