public async Task <ServiceResponse <Packaging> > Create(AddPackagingRequest request) { try { var packaging = new Packaging { Code = GenerateCode(8), Description = request.Description, Name = request.Name }; var exist = await _baseRepository.GetByIdAndCode(packaging.Id, packaging.Code); if (exist != null) { return(new ServiceResponse <Packaging>($"A Packaging With the Provided Code and or Id Already Exist")); } var exist2 = await _baseRepository.FindOneByConditions(x => x.Name.ToLower().Equals(packaging.Name.ToLower())); if (exist2 != null) { return(new ServiceResponse <Packaging>($"A Packaging With the Provided Name Already Exist")); } await _baseRepository.Create(packaging); return(new ServiceResponse <Packaging>(packaging)); } catch (Exception ex) { return(new ServiceResponse <Packaging>($"An Error Occured While Creating The Packaging. {ex.Message}")); } }
public async Task <Packaging> AddAsync(AddPackagingRequest packaging) { try { if (packaging == null) { throw new ArgumentNullException(); } PackagingEntity entity = _mapper.Map <PackagingEntity>(packaging); var addResponse = await _context.Packagings.AddAsync(entity).ConfigureAwait(false); if (addResponse.State.Equals(EntityState.Added)) { bool created = await _context.SaveChangesAsync().ConfigureAwait(false) > 0; return(created ? _mapper.Map <Packaging>(addResponse.Entity) : null); } } catch (Exception e) { _context.DetachAll(); _logger.LogError(e, "Exception: {e} // Internal Error while adding new Packaging: {packaging}", e.Message, JsonSerializer.Serialize(packaging)); } return(null); }
public async Task <ActionResult> Create(AddPackagingRequest request) { if (!ModelState.IsValid) { Alert($"Invalid Request.", NotificationType.error, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(View()); } try { var addPackagingRequest = new AddPackagingRequest { Name = request.Name, Description = request.Description }; var result = await _packagingService.Create(addPackagingRequest); if (!result.Success) { Alert($"{result.Message}", NotificationType.info, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(View()); } Alert($"Packaging Created Successfully", NotificationType.success, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { Alert($"Error! {ex.Message}.", NotificationType.error, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(View()); } }