public async Task <IActionResult> AddCriteria([FromBody] Models.DTO.Criteria newCriteria) { if (ModelState.IsValid) { if (newCriteria.Name == null || newCriteria.Name.Length == 0) { return(StatusCode(StatusCodes.Status400BadRequest, "Criteria name is null")); } if (newCriteria.ProjectId == null) { var existsCriteria = await DbContext.Criterias.FirstOrDefaultAsync(x => x.Name == newCriteria.Name); if (existsCriteria != null) { return(StatusCode(StatusCodes.Status400BadRequest, "Criteria already exists")); } } var criteria = Mapper.Map <Models.DTO.Criteria, Models.Criteria>(newCriteria); DbContext.Criterias.Add(criteria); await DbContext.SaveChangesAsync(); return(Ok()); } return(BadRequest(ModelState)); }
public async Task <IActionResult> UpdateCriteria(long id, [FromBody] Models.DTO.Criteria criteriaData) { var criteria = DbContext.Criterias.FirstOrDefault(x => x.Id == id); if (criteria == null) { return(StatusCode(StatusCodes.Status404NotFound, id)); } var newCriteria = Mapper.Map <Models.DTO.Criteria, Models.Criteria>(criteriaData); newCriteria.Id = id; DbContext.Criterias.Update(newCriteria); await DbContext.SaveChangesAsync(); return(Ok(id)); }