public async Task <ActionResult> CreatePackagingType(AddPackagingTypeViewModel request) { if (!ModelState.IsValid) { Alert($"Invalid Request.", NotificationType.error, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(RedirectToAction(nameof(PackagingTypes), new { id = request.PackagingId })); } try { var addpackagingTypeRequest = new AddPackagingTypeRequest { PackagingId = request.PackagingId, Name = request.Name, Description = request.Description }; var result = await _packagingTypeService.Create(addpackagingTypeRequest); if (!result.Success) { Alert($"{result.Message}", NotificationType.info, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(RedirectToAction(nameof(PackagingTypes), new { id = request.PackagingId })); } Alert($"Packaging Type Created Successfully", NotificationType.success, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(RedirectToAction(nameof(PackagingTypes), new { id = request.PackagingId })); } catch (Exception ex) { Alert($"Error! {ex.Message}.", NotificationType.error, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(RedirectToAction(nameof(PackagingTypes), new { id = request.PackagingId })); } }
public async Task <ServiceResponse <PackagingType> > Create(AddPackagingTypeRequest request) { try { var packaging = await _packagingTypeService.FindByIdInclusive(request.PackagingId, x => x.Include(p => p.PackagingTypes)); if (!packaging.Success) { return(new ServiceResponse <PackagingType>($"The Packaging type does not exist")); } if (packaging.Data.PackagingTypes.Count > 0 && packaging.Data.PackagingTypes.Any(x => x.Name.ToLower().Equals(request.Name.ToLower()))) { return(new ServiceResponse <PackagingType>($"The Packaging already exist exist")); } var packagingType = new PackagingType { Name = request.Name, Code = GenerateCode(8), Description = request.Description, PackagingId = packaging.Data.Id }; var exist = await _baseRepository.GetByIdAndCode(packagingType.Id, packagingType.Code); if (exist != null) { return(new ServiceResponse <PackagingType>($"A Packaging Type With the Provided Code and or Id Already Exist")); } var exist2 = await _baseRepository.FindOneByConditions(x => x.Name.ToLower().Equals(packagingType.Name.ToLower())); if (exist2 != null) { return(new ServiceResponse <PackagingType>($"A Packaging Type With the Provided Name Already Exist")); } await _baseRepository.Create(packagingType); return(new ServiceResponse <PackagingType>(packagingType)); } catch (Exception ex) { return(new ServiceResponse <PackagingType>($"An Error Occured While Creating The Packaging Type. {ex.Message}")); } }