public void SendLike(User user, int chatMessageId, bool turnOn, bool up) { Like likeUp = _context.Likes.FirstOrDefault(p => p.User == user && p.ChatMessageId == chatMessageId); if (likeUp == null) { likeUp = new Like() { User = user, ChatMessageId = chatMessageId, LikeUp = turnOn && up ? true : false, LikeDown = turnOn && !up ? true : false, LikeCount = turnOn ? (up ? 1 : -1) : 0 }; _context.Likes.Add(likeUp); _context.SaveChanges(); } else { if (up && turnOn != likeUp.LikeUp || !up && turnOn != likeUp.LikeDown) { likeUp.LikeUp = turnOn && up ? true : false; likeUp.LikeDown = turnOn && !up ? true : false; likeUp.LikeCount = turnOn ? (up ? 1 : -1) : 0; _context.Likes.Update(likeUp); _context.SaveChanges(); } } }
public FileModel AddPhoto(IFormFile file) { FileModel photo = new FileModel(); string newFileName = string.Empty; string PathDB = string.Empty; if (file.Length > 0) { string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); string myUniqueFileName = Convert.ToString(Guid.NewGuid()); string FileExtension = Path.GetExtension(fileName); newFileName = myUniqueFileName + FileExtension; fileName = Path.Combine(_environment.WebRootPath, "Files") + $@"\{newFileName}"; PathDB = "Files/" + newFileName; using (FileStream fs = System.IO.File.Create(fileName)) { file.CopyTo(fs); fs.Flush(); } } photo.Name = newFileName; photo.Path = PathDB; _context.Files.Add(photo); _context.SaveChanges(); return(photo); }
public int AddNews(News news) { UpdateNewsTags(news); _context.News.Add(news); _context.SaveChanges(); return(news.Id); }