public ActionResult <DiaryUserDto> PutStudent(long id, Diary diary) { User user = GetCurrentUser(); var result = _context.Diary.Include(q => q.User).SingleOrDefault(o => o.Id == id); if (result == null || result.User != user) { return(BadRequest(new { msg = "没有权限操作!" })); } result.Title = diary.Title; result.Content = diary.Content; result.IsPublic = diary.IsPublic; _context.Update(result); _context.SaveChanges(); //diary.Id = result.Id; //_context.Entry(diary).State = Microsoft.EntityFrameworkCore.EntityState.Modified; //_context.Entry(diary).Property(x => x.CreateTime).IsModified = false; //_context.Entry(diary).Property(x => x.UserId).IsModified = false; //try //{ // _context.SaveChanges(); //} //catch (DbUpdateConcurrencyException) //{ // return NotFound(); //} return(CreatedAtAction(nameof(Get), new { id = diary.Id }, ModelToDtoUtil.GetDiaryUserDto(result, user))); }
public ActionResult <DiaryUserDto> Post([FromBody] Diary diary) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } User user = GetCurrentUser(); diary.UserId = user.Id; diary.CreateTime = DateTime.Now; _context.Diary.Add(diary); _context.SaveChanges(); return(CreatedAtAction(nameof(Get), new { id = diary.Id }, ModelToDtoUtil.GetDiaryUserDto(diary, user))); }
public ActionResult <DiaryUserDto> Delete(long id) { User user = GetCurrentUser(); var result = _context.Diary.Include(q => q.User).SingleOrDefault(o => o.Id == id); if (result == null || result.User != user) { return(BadRequest(new { msg = "没有权限操作!" })); } var temp = _context.Diary.Find(id); _context.Diary.Remove(temp); _context.SaveChanges(); return(ModelToDtoUtil.GetDiaryUserDto(result, user)); }
public ActionResult <DiaryWithPageInfoDto> GetDiaryByUser([FromQuery] PageDto pageInfo) { User user = GetCurrentUser(); //Console.WriteLine(user.Id); var diaries = _context.Diary.Include(q => q.User) .Where(i => i.UserId == user.Id); pageInfo.Total = diaries.Count(); var result = diaries .OrderByDescending(t => t.CreateTime) .Skip(pageInfo.PageSize * (pageInfo.PageIndex - 1)) .Take(pageInfo.PageSize) .ToList(); List <DiaryUserDto> dto = new List <DiaryUserDto>(); result.ForEach(d => dto.Add(ModelToDtoUtil.GetDiaryUserDto(d, d.User))); //_context.Diary.Include(q => q.User).ToList() return(ModelToDtoUtil.GetDiaryWithPageInfoDto(dto, pageInfo)); }
public ActionResult <DiaryUserDto> Get(long id) { //var diary = _context.Diary.Find(id); var diary = _context.Diary.Include(q => q.User).SingleOrDefault(o => o.Id == id); if (diary == null) { return(NotFound()); } User user = GetCurrentUser(); if (diary.IsPublic == false && diary.User != user) { return(BadRequest(new { msg = "没有权限查看!" })); } Console.WriteLine(diary.User.Username); return(ModelToDtoUtil.GetDiaryUserDto(diary, diary.User)); //return diary; }
public ActionResult <DiaryWithPageInfoDto> Get([FromQuery] PageDto pageInfo) { var diaries = _context.Diary.Include(q => q.User); pageInfo.Total = diaries.Count(); var result = diaries .Where(p => p.IsPublic == true) .OrderByDescending(t => t.CreateTime) .Skip(pageInfo.PageSize * (pageInfo.PageIndex - 1)) .Take(pageInfo.PageSize) .ToList(); List <DiaryUserDto> dto = new List <DiaryUserDto>(); //foreach(var d in diaries) //{ // Console.WriteLine(d.User.Username); // dto.Add(ModelToDtoUtil.GetDiaryUserDto(d, d.User));//apend 无法实现 //} result.ForEach(d => dto.Add(ModelToDtoUtil.GetDiaryUserDto(d, d.User))); //_context.Diary.Include(q => q.User).ToList() return(ModelToDtoUtil.GetDiaryWithPageInfoDto(dto, pageInfo)); }