public async Task <IActionResult> InsertAppProducts(AppProductsCreateDto appProductsCreateDto) { var response = await _appProductsService.InsertProducts(appProductsCreateDto); return(Ok(response)); }
public async Task <ApiResponse <AppProductsGetDto> > InsertProducts(AppProductsCreateDto appProductsCreateDto) { AppProductsGetDto resultDto = new AppProductsGetDto(); Metadata metadata = new Metadata { IsValid = true, Message = "" }; ApiResponse <AppProductsGetDto> response = new ApiResponse <AppProductsGetDto>(resultDto); try { if (appProductsCreateDto.UrlImage == null) { metadata.IsValid = false; metadata.Message = "Url Imagen no valida"; response.Meta = metadata; response.Data = resultDto; return(response); } if (appProductsCreateDto.ExternalCode == "") { metadata.IsValid = false; metadata.Message = "Codigo Externo no es valido"; response.Meta = metadata; response.Data = resultDto; return(response); } AppProducts appProductsFind = await GetByCode(appProductsCreateDto.Code); if (appProductsFind != null) { metadata.IsValid = false; metadata.Message = "Codigo de producto ya existe, verifique por favor"; response.Meta = metadata; response.Data = resultDto; return(response); } AppUnits appUnitsFind = await _appUnitsService.GetById((int)appProductsCreateDto.AppUnitsId); if (appUnitsFind == null) { metadata.IsValid = false; metadata.Message = "Unidad venta del Producto No existe"; response.Meta = metadata; response.Data = resultDto; return(response); } appUnitsFind = await _appUnitsService.GetById((int)appProductsCreateDto.ProductionUnitId); if (appUnitsFind == null) { metadata.IsValid = false; metadata.Message = "Unidad Produccion del Producto No existe"; response.Meta = metadata; response.Data = resultDto; return(response); } MtrTipoMoneda mtrTipoMonedaFind = await _unitOfWork.MtrTipoMonedaRepository.GetById((long)appProductsCreateDto.PrymaryMtrMonedaId); if (mtrTipoMonedaFind == null) { metadata.IsValid = false; metadata.Message = "Moneda Primaria No Existe!!! "; response.Meta = metadata; response.Data = resultDto; return(response); } mtrTipoMonedaFind = await _unitOfWork.MtrTipoMonedaRepository.GetById((long)appProductsCreateDto.SecundaryMtrMonedaId); if (mtrTipoMonedaFind == null) { metadata.IsValid = false; metadata.Message = "Moneda Secundaria No Existe!!! "; response.Meta = metadata; response.Data = resultDto; return(response); } AppSubCategory appSubCategory = await _unitOfWork.AppSubCategoryRepository.GetById((int)appProductsCreateDto.AppSubCategoryId); if (appSubCategory == null) { metadata.IsValid = false; metadata.Message = "Subcategoria No Existe!!! "; response.Meta = metadata; response.Data = resultDto; return(response); } AppProducts appProductsCreate = _mapper.Map <AppProducts>(appProductsCreateDto); appProductsCreate.CreatedAt = DateTime.Now; appProductsCreate.UserCreate = appProductsCreateDto.UsuarioConectado; appProductsCreate.UserUpdate = appProductsCreateDto.UsuarioConectado; appProductsCreate.UpdatedAt = DateTime.Now; if (appProductsCreate.VariablesSearchText == null) { appProductsCreate.VariablesSearchText = ""; } AppProducts appProductsInserted = await Insert(appProductsCreate); if (appProductsInserted != null) { resultDto = _mapper.Map <AppProductsGetDto>(appProductsInserted); AppUnits AppUnitsFind = await _appUnitsService.GetById((int)appProductsInserted.AppUnitsId); if (AppUnitsFind != null) { AppUnitsGetDto appUnitsGetDto = _mapper.Map <AppUnitsGetDto>(AppUnitsFind); resultDto.AppUnitsGetDto = appUnitsGetDto; } if (resultDto.UrlImage == "" || resultDto.UrlImage == null) { resultDto.Link = _paginationOptions.UrlGetFiles + "NoImage.png"; } else { resultDto.Link = _paginationOptions.UrlGetFiles + resultDto.UrlImage; } metadata.IsValid = true; metadata.Message = $"Producto: {resultDto.Code} creado Satisfactoriamente!!"; } else { metadata.IsValid = false; metadata.Message = "Registro No actualizado"; } response.Meta = metadata; response.Data = resultDto; return(response); } catch (Exception ex) { metadata.IsValid = false; metadata.Message = ex.InnerException.Message; response.Meta = metadata; response.Data = resultDto; return(response); } }