public StoreContactDto Create(int storeID, CreateStoreContactDto dto) { _dbContext.ValidateData(dto, storeID); var entity = dto.ToEntity(); entity.StoreID = storeID; _dbContext.StoreContact.Add(entity); _dbContext.SaveChanges(); return(entity.ToDto()); }
public IActionResult Create([FromRoute] int storeID, [FromBody] CreateStoreContactDto dto) { try { var result = _repo.Create(storeID, dto); return(StatusCode((int)HttpStatusCode.Created, result)); } catch (Exception ex) { _logger.LogError(ex.Message); return(BadRequest(ex.Message)); } }
public static void ValidateData(this IHDSContext context, CreateStoreContactDto dto, int id) { var errors = new StringBuilder(); // StoreID errors.AddIfExists(context.KeyExists <Store>(id, ValidationMessages.StoreIDNotFound)); // ContactMethodTypeID errors.AddIfExists(dto.ContactMethodTypeID.ValidateRequired(ValidationMessages.ContactMethodTypeIDRequired)); errors.AddIfExists(context.KeyExists <ContactMethodType>(dto.ContactMethodTypeID, ValidationMessages.ContactMethodTypeIDNotFound)); // ContactMethod Value errors.AddIfExists(dto.ContactMethodValue.ValidateRequired(ValidationMessages.ContactMethodValueRequired)); errors.AddIfExists(dto.ContactMethodValue.ValidateLength(100, ValidationMessages.ContactMethodValueLength)); if (errors.Length > 0) { throw new ValidationException(errors.ToString()); } }
public static StoreContact ToEntity(this CreateStoreContactDto dto) { Init(); return(Mapper.Map <StoreContact>(dto)); }