public ActionResult Add(WorkPostDTO dto) { if (!ModelState.IsValid) { return(Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) })); } var data = workPostService.Add(dto); if (data > 0) { return(Json(new AjaxResult { Status = "ok" })); } else { return(Json(new AjaxResult { Status = "error", ErrorMsg = "添加失败!" })); } }
public async Task <IActionResult> CreateWork(WorkPostDTO workToBeCreated) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check if author exists. Author existingAuthor = await _db.Authors.FirstOrDefaultAsync(a => a.AuthorId == workToBeCreated.AuthorId); if (existingAuthor == null) { return(BadRequest(new { message = _errorMessageAuthorNotExists })); } // Check if work already exists. Work existingWork = await _db.Works .FirstOrDefaultAsync(w => w.Title == workToBeCreated.Title); if (existingWork != null) { return(Conflict(new { message = _errorMessageWorkExists })); } try { int userId = (int)_userCtx.GetId(); Work newWork = WorkPostDTO.ToModel(workToBeCreated, userId); await _db.Works.AddAsync(newWork); await _db.SaveChangesAsync(); // Prepare response. WorkGetDTO newWorkResponse = WorkGetDTO.FromModel(newWork); // Trigger webhook for new work. TriggerWorkWebhook(Event.EditedWork, newWorkResponse, userId); string locationUri = $"{_baseUrl}/api/v1/works/{newWork.WorkId}"; return(Created(locationUri, WorkGetDTO.FromModel(newWork))); } catch (DbUpdateException) { return(StatusCode(StatusCodes.Status500InternalServerError, _errorMessageSavingData)); } }
public int Add(WorkPostDTO dto) { using (MyDbContext ctx = new MyDbContext()) { WorkPostEntity ef = new WorkPostEntity(); ef.Position = dto.Position; ef.Address = dto.Address; ef.CountNum = dto.CountNum; ef.Email = dto.Email; ef.Manager = dto.Manager; ef.Wage = dto.Wage; ef.ProjectId = dto.ProjectId; ef.Score = dto.Score; ctx.WorkPosts.Add(ef); ctx.SaveChanges(); return(ef.Id); } }
public WorkPostDTO ToDTO(WorkPostEntity ef) { WorkPostDTO dto = new WorkPostDTO(); dto.Address = ef.Address; dto.CountNum = ef.CountNum; dto.CreateDateTime = ef.CreateDateTime; dto.Email = ef.Email; dto.Id = ef.Id; dto.Manager = ef.Manager; dto.Position = ef.Position; dto.Wage = ef.Wage; dto.ProjectId = ef.ProjectId; dto.ProjectTitle = ef.Project.Title; dto.Score = ef.Score; return(dto); }