public ActionResult createAlbumByID(string albumIntro, string albumName, string userID) { DateTime dateTime = DateTime.Now; //创建当前时间 int createAlbumFlag = 0; Album album = new Album(); album.AlbumIntroduction = albumIntro; album.AlbumName = albumName; album.UserId = int.Parse(userID); album.AlbumTime = dateTime; try { entity.Album.Add(album); entity.SaveChanges(); entity.Entry(album); //返回插入的记录并注入到album createAlbumFlag = 1; } catch (Exception e) { Console.WriteLine(e.Message); } if (createAlbumFlag == 1) { string newPath = Path.Combine(albumRootPath, userID, album.AlbumId.ToString()); if (!Directory.Exists(newPath)) //创建相簿文件目录 { Directory.CreateDirectory(newPath); } } return(Json(new { createAlbumFlag = createAlbumFlag, albumID = album.AlbumId.ToString() })); }
public async Task <SystemResponse> UpdatePost(Post updatePost) { Post postObject = _dataContext.Set <Post>() .AsTracking() .FirstOrDefault(p => p.Id == updatePost.Id); try { if (postObject == null) { throw new RecordNotFoundException($"Post is not found {updatePost.Id}"); } _dataContext.Entry(postObject).CurrentValues.SetValues(updatePost); await _dataContext.SaveChangesAsync(); return(SystemResponse.SuccessResponse("Success")); } catch (DbUpdateException ex) { _logger.LogError(ex, "Update post failed"); _dataContext.Entry(postObject).Reload(); throw new UpdateFailedException("Update post failed"); } catch (Exception ex) { _logger.LogError(ex, "Update post failed"); throw new UpdateFailedException("Update post failed"); } }
public ActionResult ArticleEdit(Article article, int?id, HttpPostedFileBase postedFile) { int PcId = Convert.ToInt32(article.PictureId); var pc = blogContext.Picture.Where(a => a.PictureId == PcId).SingleOrDefault(); if (postedFile != null) { System.IO.File.Delete(Server.MapPath("~/Images/" + pc.Path)); var dt = Guid.NewGuid().ToString() + "_" + Path.GetFileName(postedFile.FileName); string filePath = Path.Combine(Server.MapPath("~/Images/"), dt); postedFile.SaveAs(filePath); pc.Path = dt; blogContext.Entry(pc).State = EntityState.Modified; } blogContext.Entry(article).State = EntityState.Modified; blogContext.SaveChanges(); return(RedirectToAction("Article", "Admin")); }
public JsonResult personalInfo(string nick_name, string gender, string location, string year, string month, string day, string email, string tel, string wechat) { //User user = new User(); int flag = 0; var userid = (from c in entity.User where c.NickName == nick_name select c.UserId).Distinct(); var id = userid.FirstOrDefault(); var user = entity.User.Find(id); //在数据库中根据key找到相应记录 if (id == default) { flag = 0; //没有找到该用户 } else { //user.UserId = id; user.Gender = gender; user.Location = location; //DateTime dateOfBirth = new DateTime(); //string.Format("yyyy年MM月dd日", dateOfBirth); //user.Dob = dateOfBirth; var yyyy = year; var mm = month; var dd = day; DateTime dateTime = new DateTime(); DateTime.TryParse(yyyy + "-" + mm + "-" + dd, out dateTime); user.Dob = dateTime; user.Email = email; user.Tel = tel; user.Wechat = wechat; //entity.User.Add(user); //把user这个实体加入数据库 entity.Entry(user).State = EntityState.Modified; entity.SaveChanges(); flag = 1; //信息存储完成 } var data = new { Infoflag = flag }; if (flag == 0) { Response.StatusCode = 404;//没有找到该用户 return(Json(data)); } else { Response.StatusCode = 200; //成功修改信息 return(Json(data)); } }
public ActionResult createTopicByID() { DateTime dateTime = DateTime.Now; //获取当前时间 Topic topic = new Topic(); //新建topic条目项 int createTopicFlag = 0; try { //获取参数 string content = Request.Form["content"]; string title = Request.Form["title"]; string userID = Request.Form["userID"]; string zoneID = Request.Form["zoneID"]; //向条目中插入数据 topic.TopicContent = content; topic.TopicTitle = title; topic.AnswerNum = 0; topic.UserId = int.Parse(userID); topic.TopicUploadTime = dateTime; topic.ZoneId = int.Parse(zoneID); var zoneContent = (from c in entity.Zone where c.ZoneId == int.Parse(zoneID) select c).FirstOrDefault(); zoneContent.ZoneTopicNum += 1; entity.Zone.Update(zoneContent); entity.SaveChanges(); entity.Add(topic); createTopicFlag = entity.SaveChanges(); //存入数据库 返回值为1表示成功 entity.Entry(topic); createTopicFlag = 1; return(Json(new { topicDetail = topic, createTopicFlag = createTopicFlag })); } catch (Exception e) { Console.WriteLine(e.Message); createTopicFlag = 0; return(Json(new { createTopicFlag = createTopicFlag })); } }
public JsonResult addArticleByID(string nick_name, string folderName, int articleID) { var flag = 0; //根据用户名找到用户ID var userid = (from c in entity.User where c.NickName == nick_name select c.UserId).Distinct(); var id = userid.FirstOrDefault(); string msg = ""; if (id == default) { flag = 2; //Response.StatusCode = 405;//没找到该用户 return(Json(new { ReturnFlag = flag, UserID = id, result = "NOT FOUND" })); } var folderid = (from c in entity.Favourite where (c.FavouriteName == folderName && c.UserId == id) select c.FavouriteId).Distinct(); var F_id = folderid.FirstOrDefault(); if (F_id == default) { //Response.StatusCode = 404;//没找到该文件夹 flag = 3; return(Json(new { ReturnFlag = flag, folderID = F_id, result = "NOT FOUND" })); } try { //FAVOURITE里面更新收藏夹文章数量 var folder = entity.Favourite.Find(F_id); var num = folder.ArticleNum; if (num == default) { folder.ArticleNum = 1; } else { folder.ArticleNum = num + 1; } entity.Entry(folder).State = EntityState.Modified; //entity.SaveChanges(); //ARTICLE表里面更新文章收藏量 var article = entity.Article.Find(articleID); if (article == default) { //Response.StatusCode = 400;//没有该文章 flag = 4; return(Json(new { ReturnFlag = flag, ArticleID = articleID, result = "Article NOT FOUND!" })); } var collectNum = article.CollectNum; if (collectNum == default) { article.CollectNum = 1; } else { article.CollectNum = collectNum + 1; } entity.Entry(article).State = EntityState.Modified; //entity.SaveChanges(); //加入FAVOURITE_ARTICLE表 var item = new FavouriteArticle(); item.FavouriteId = F_id; item.ArticleId = articleID; item.FavouriteTime = DateTime.Now; entity.FavouriteArticle.Add(item); entity.SaveChanges(); //Response.StatusCode = 200;//成功 flag = 1; return(Json(new { ReturnFlag = flag, folderID = F_id, articleId = articleID })); } catch (Exception e) { flag = 0; msg = e.Message; } return(Json(new { Flag = flag, errorMsg = msg })); }