public static void ValidateData(this IHDSContext context, CreateStoreRoleDto dto) { var errors = new StringBuilder(); // RoleDescription errors.AddIfExists(dto.RoleDescription.ValidateRequired(ValidationMessages.RoleDescriptionRequired)); errors.AddIfExists(dto.RoleDescription.ValidateLength(400, ValidationMessages.RoleDescriptionLength)); // StoreID errors.AddIfExists(dto.StoreID.ValidateRequired(ValidationMessages.StoreIDRequired)); errors.AddIfExists(context.KeyExists <Store>(dto.StoreID, ValidationMessages.StoreIDNotFound)); if (dto.EmployeePositions != null && dto.EmployeePositions.Any()) { // EmployeePositions for (int i = 0; i < dto.EmployeePositions.Count; i++) { // EmployeeID errors.AddIfExists(dto.EmployeePositions[i].EmployeeID.ValidateRequired($"EmployeePosition [{i}]:{ValidationMessages.EmployeeIDRequired}")); errors.AddIfExists(context.KeyExists <Employee>(dto.EmployeePositions[i].EmployeeID, $"EmployeePosition [{i}]:{ValidationMessages.EmployeeIDNotFound}")); } } if (errors.Length > 0) { throw new ValidationException(errors.ToString()); } }
public StoreRoleDto Create(CreateStoreRoleDto dto) { _dbContext.ValidateData(dto); var storeRole = dto.ToEntity(); _dbContext.StoreRole.Add(storeRole); _dbContext.SaveChanges(); return(storeRole.ToDto()); }
public IActionResult Create([Required][FromBody] CreateStoreRoleDto storeRole) { try { var result = _repo.Create(storeRole); return(StatusCode((int)HttpStatusCode.Created, result)); } catch (Exception ex) { _logger.LogError(ex.Message); return(BadRequest(ex.Message)); } }
public static StoreRole ToEntity(this CreateStoreRoleDto dto) { Init(); return(Mapper.Map <StoreRole>(dto)); }