public IActionResult likePicute(string userId, string picId, string type) { //首先查找是否有现有的点赞 var like = services.getLikes(userId, picId); var pic = services.getPicture(picId); if (like != null)//现在已经有 赞 { //点击类型相同,取消赞 context.likesPicture.Remove(like); pic.likes--;//原有的赞减少 context.picture.Attach(pic); context.SaveChanges(); } else {//创建新的赞 like = new likespicture { u_id = userId, p_id = picId, like_time = DateTime.Now, like_type = type }; context.likesPicture.Add(like); pic.likes++;//原有的赞增加 context.Attach(pic); context.SaveChanges(); } return(Ok(new { Success = true, msg = "Operation Done" })); }
public async Task <IActionResult> SimilarPicture([FromForm] IFormCollection forms) { var files = Request.Form.Files; long size = files.Sum(f => f.Length); //size > 100MB refuse upload ! if (size > 104857600) { return(Ok(new { Success = false, Message = "pictures total size > 100MB , server refused !" })); } var file = files[0]; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); string filePath = "C:" + @"\Pics\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string suffix = fileName.Split('.')[1]; //检查文件后缀名确保是图片而不是其他文件。 if (!pictureFormatArray.Contains(suffix)) { return(Ok(new { Success = false, Message = "the picture format not support ! you must upload files " + "that suffix like 'png','jpg','jpeg','bmp','gif','ico'.", Name = fileName })); } fileName = Guid.NewGuid() + "." + suffix; string fileFullName = filePath + fileName; int height = 0; int width = 0; await using (FileStream fs = System.IO.File.Create(fileFullName)) { file.CopyTo(fs); System.Drawing.Image image = System.Drawing.Image.FromStream(fs); height = image.Height; width = image.Width; fs.Flush(); } string message = $"{files.Count} file(s) /{size} bytes uploaded successfully!"; //刷新为服务器的图片。 fileFullName = "C:/Pics/" + fileName; //string[] ReturnTag = PictureServices.getTag("D:/Entertainment/Entertainment/MechineLearning/Data/test/5.jpg"); string[] ReturnTag = PictureServices.getTag(fileFullName); var returnList = new List <picInfo> { }; //循环3个tag for (int i = 0; i < 3; ++i) { var picList = context.ownTag.ToLookup(t => t.tag_name)[ReturnTag[i]].ToList(); //循环每个tag里面的图片 foreach (var pic in picList) { var nowPic = PictureServices.getPicture(pic.p_id); var pubulisher = services.GetUserById(context.publishPicture .FirstOrDefault(p => p.p_id == nowPic.p_id).u_id); var newPicInfo = new picInfo { picId = nowPic.p_id, picUrl = nowPic.p_url, publisherId = pubulisher.u_id, publisherName = pubulisher.u_name, info = nowPic.p_info, starNum = context.favoritePicture.Count(f => f.p_id == nowPic.p_id), likeNum = context.likesPicture.Count(l => l.p_id == nowPic.p_id), commNum = context.picComment.Count(c => c.p_id == nowPic.p_id) }; returnList.Add(newPicInfo); } } return(Ok(new { Success = true, picList = returnList, msg = "Operation Done" })); }