public Dictionary <string, string> Validate(SourceCategoryTarget model) { var modelErrors = new Dictionary <string, string>(); var duplicateCount = dbContext.SourceCategoryTargets.AsNoTracking().Count(a => a.SourceCategoryId == model.SourceCategoryId && a.DT == model.DT && a.SourceCategoryTargetId != model.SourceCategoryTargetId); if (duplicateCount > 0) { modelErrors.Add(nameof(model.SourceCategoryId), "Entry already exists"); } return(modelErrors); }
public SourceCategoryTarget Create(SourceCategoryTarget model) { var weekDetail = new WeekDetail(model.DT); model.WeekDay = weekDetail.WeekDay; model.WeekNum = weekDetail.WeekNum; model.FirstDay = weekDetail.FirstDay; model.LastDay = weekDetail.LastDay; dbContext.SourceCategoryTargets.Add(model); dbContext.SaveChanges(); return(model); }
private bool validateEntity(SourceCategoryTarget model) { var modelErrors = repository.Validate(model); if (modelErrors.Count > 0) { foreach (var modelError in modelErrors) { ModelState.AddModelError(modelError.Key, modelError.Value); } } return(ModelState.ErrorCount == 0); }
public SourceCategoryTarget Update(SourceCategoryTarget model) { var entity = dbContext.SourceCategoryTargets.Find(model.SourceCategoryTargetId); if (entity == null) { throw new Exception("Selected Record does not exists."); } entity.SourceCategoryId = model.SourceCategoryId; entity.Wt = model.Wt; dbContext.SourceCategoryTargets.Update(entity); dbContext.SaveChanges(); return(entity); }
public IActionResult Put([FromBody] SourceCategoryTarget model) { try { if (!ModelState.IsValid) { return(InvalidModelStateResult()); } if (!validateEntity(model)) { return(InvalidModelStateResult()); } return(Accepted(repository.Update(model))); } catch (Exception ex) { logger.LogError(ex.GetExceptionMessages()); return(StatusCode(StatusCodes.Status500InternalServerError, Constants.ErrorMessages.UpdateError)); } }
public bool Delete(SourceCategoryTarget model) { dbContext.SourceCategoryTargets.Remove(model); dbContext.SaveChanges(); return(true); }