public async Task <IActionResult> PostOrder([FromBody] OrderPostInfo orderInfo) { DateTime generateTime = DateTime.Now; // 生成 GUID 并取前四位作为密码 string password = Guid.NewGuid().ToString().Split('-')[1]; List <File> filesList = new List <File>(); // 判断传来的 fileId 是否存在,存在则建立与 order 实体的所属关系 foreach (var fileId in orderInfo.files) { var file = await _context.File.SingleOrDefaultAsync(m => m.ID == fileId); if (file != null && file.Order == null) { filesList.Add(file); } } if (filesList.Count < 1) { return(BadRequest()); } Order order = new Order { GenerateTime = generateTime, Password = password, Files = filesList }; // 与数据库同步 _context.Order.Add(order); await _context.SaveChangesAsync(); return(Ok(new OrderReturnInfo(order, true))); }
public async Task <IHttpActionResult> PutFileUpload(int id, FileUpload fileUpload) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != fileUpload.imageid) { return(BadRequest()); } db.Entry(fileUpload).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FileUploadExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> DeleteFile([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var file = await _context.File.SingleOrDefaultAsync(m => m.ID == id); if (file == null) { return(NotFound()); } _context.File.Remove(file); await _context.SaveChangesAsync(); return(Ok(new FileReturnInfo(file))); }
public async Task <bool> SaveAsync() { return(await _context.SaveChangesAsync() > 0); }