protected override async Task <EntityResponseModel <TReadModel> > ProcessAsync(EntityUpdateCommand <TKey, TUpdateModel, EntityResponseModel <TReadModel> > request, CancellationToken cancellationToken) { var entityResponse = new EntityResponseModel <TReadModel>(); try { var dbSet = DataContext.Set <TEntity>(); var keyValue = new object[] { request.Id }; var entity = await dbSet.FindAsync(keyValue, cancellationToken).ConfigureAwait(false); if (entity == null) { return(default(EntityResponseModel <TReadModel>)); } Mapper.Map(request.Model, entity); await DataContext .SaveChangesAsync(cancellationToken) .ConfigureAwait(false); var readModel = await Read(entity.Id, cancellationToken); entityResponse.ReturnStatus = true; entityResponse.Data = readModel; } catch (Exception ex) { entityResponse.ReturnMessage.Add(String.Format("Unable to Update Record {0}" + ex.Message, typeof(TEntity).Name)); entityResponse.ReturnStatus = false; } return(entityResponse); }
public async Task <EntityResponseModel <ApplicantModel> > Delete(int Id) { var response = new EntityResponseModel <ApplicantModel>(); try { var applicant = await DataContext.Applicant .Where(s => s.ID == Id) .FirstOrDefaultAsync() .ConfigureAwait(false); if (applicant == null) { throw new Exception("Record not found"); } DataContext.Applicant.Remove(applicant); DataContext.SaveChanges(); response.ReturnMessage.Add("Deleted Succesfully"); response.Data = Mapper.Map <ApplicantModel>(applicant); } catch (Exception ex) { response.ReturnMessage.Add(ex.Message); response.ReturnStatus = false; } return(response); }
public async Task <EntityResponseModel <ApplicantModel> > Update(ApplicantModel model) { var response = new EntityResponseModel <ApplicantModel>(); try { var Validate = CheckValidation(model); if (!Validate.ReturnStatus) { return(Validate); } if (!DataContext.Applicant.Any(s => s.ID == model.ID)) { throw new Exception("Record not found"); } var applicant = Mapper.Map <Hahn.ApplicatonProcess.December2020.Data.Entities.Applicant>(model); applicant.ID = model.ID; DataContext.Applicant.Update(applicant); await DataContext.SaveChangesAsync(); response.Data = Mapper.Map <ApplicantModel>(applicant); } catch (Exception ex) { response.ReturnMessage.Add(ex.Message); response.ReturnStatus = false; } return(response); }
public async Task <EntityResponseModel <ApplicantModel> > Insert(ApplicantModel model) { var response = new EntityResponseModel <ApplicantModel>(); try { var Validate = CheckValidation(model); if (!Validate.ReturnStatus) { return(Validate); } var applicant = Mapper.Map <Data.Entities.Applicant>(model); await DataContext.Applicant.AddAsync(applicant); DataContext.SaveChanges(); response.Data = Mapper.Map <ApplicantModel>(applicant); } catch (Exception ex) { response.ReturnMessage.Add(ex.Message); response.ReturnStatus = false; } return(response); }
private static async Task <EntityResponseModel <TResponse> > Errors(IEnumerable <ValidationFailure> failures) { var response = new EntityResponseModel <TResponse>(); foreach (var failure in failures) { response.Errors.Add(failure.PropertyName, failure.ErrorMessage); } response.ReturnStatus = false; response.ReturnMessage.Add("Validation Error"); return(await Task.FromResult(response)); }
protected override async Task <EntityResponseModel <TReadModel> > ProcessAsync(EntityCreateCommand <TCreateModel, EntityResponseModel <TReadModel> > request, CancellationToken cancellationToken) { var EntityResponse = new EntityResponseModel <TReadModel>(); var dbSet = DataContext.Set <TEntity>(); var entiy = Mapper.Map <TEntity>(request.Model); dbSet.Add(entiy); await DataContext .SaveChangesAsync(cancellationToken) .ConfigureAwait(false); var readModel = Mapper.Map <TReadModel>(entiy); EntityResponse.ReturnStatus = true; EntityResponse.Data = readModel; return(EntityResponse); }
protected override async Task <EntityResponseModel <TReadModel> > ProcessAsync(EntityIdentifierQuery <TKey, EntityResponseModel <TReadModel> > request, CancellationToken cancellationToken) { var entityResponse = new EntityResponseModel <TReadModel>(); try { var model = await Read(request.Id, request.IncludeProperties, cancellationToken); entityResponse.ReturnStatus = true; entityResponse.Data = model; } catch (Exception ex) { entityResponse.ReturnMessage.Add(String.Format("Unable to Get Record from {0} - with Id {1}" + ex.Message, typeof(TEntity).Name, request.Id.ToString())); entityResponse.ReturnStatus = false; } return(entityResponse); }
protected override async Task <EntityResponseModel <TReadModel> > ProcessAsync(EntityDeleteCommand <TKey, EntityResponseModel <TReadModel> > request, CancellationToken cancellationToken) { var entityResponse = new EntityResponseModel <TReadModel>(); try { var dbSet = DataContext .Set <TEntity>(); var keyValue = new object[] { request.Id }; var entity = dbSet.Where(p => Equals(p.Id, request.Id)); if (!string.IsNullOrEmpty(request.IncludeProperties)) { entity = request.IncludeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Aggregate(entity, (current, includeProperty) => current.Include(includeProperty.Trim(new char[] { ' ', '\n', '\r' }))); } var model = entity.FirstOrDefault(); if (model == null) { return(default);
protected override async Task <EntityResponseModel <ProductImageReadDto> > ProcessAsync(EntityCreateCommand <ProductImageCreateDto, EntityResponseModel <ProductImageReadDto> > request, CancellationToken cancellationToken) { EntityResponseModel <ProductImageReadDto> response = new EntityResponseModel <ProductImageReadDto>(); try { var current = Mapper.Map <DOMAIN.Entities.ProductImage>(request.Model); var files = new byte[] { }; if (request.Model.File == null) { throw new Exception("Product Image is required.,"); } if (request.Model.File != null) { if (request.Model.File.Length > 0) { var file = request.Model.File; using var ms = new MemoryStream(); file.CopyTo(ms); current.ImageData = ms.ToArray(); } } var dbSet = DataContext.Set <DOMAIN.Entities.ProductImage>(); await dbSet .AddAsync(current, cancellationToken) .ConfigureAwait(false); await DataContext.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); response.ReturnStatus = true; response.Data = Mapper.Map <ProductImageReadDto>(current); } catch (Exception ex) { response.ReturnMessage.Add(String.Format("Unable to Insert Record {0}" + ex.Message, typeof(DOMAIN.Entities.ProductImage).Name)); response.ReturnStatus = false; } return(response); }
public async Task <EntityResponseModel <ApplicantModel> > GetById(int Id) { EntityResponseModel <ApplicantModel> response = new EntityResponseModel <ApplicantModel>(); try { var applicant = await DataContext.Applicant .Where(s => s.ID == Id) .AsNoTracking() .FirstOrDefaultAsync() .ConfigureAwait(false); response.Data = Mapper.Map <ApplicantModel>(applicant); } catch (Exception ex) { response.ReturnMessage.Add(ex.Message); response.ReturnStatus = false; } return(response); }
public async Task <IActionResult> Delete(CancellationToken cancellationToken, int id) { var returnResponse = new EntityResponseModel <ProductReadDto>(); try { var command = new EntityDeleteCommand <int, EntityResponseModel <ProductReadDto> >(id); var result = await Mediator.Send(command, cancellationToken).ConfigureAwait(false); if (result.ReturnStatus == false) { return(BadRequest(result)); } return(Ok(result)); } catch (Exception ex) { returnResponse.ReturnStatus = false; returnResponse.ReturnMessage.Add(ex.Message); return(BadRequest(returnResponse)); } }
public async Task <IActionResult> GetById(int id, CancellationToken cancellationToken) { var returnResponse = new EntityResponseModel <ProductReadDto>(); try { var query = new EntityIdentifierQuery <int, EntityResponseModel <ProductReadDto> >(id); var result = await Mediator.Send(query, cancellationToken).ConfigureAwait(false); if (result.ReturnStatus == false) { return(BadRequest(result)); } return(Ok(result)); } catch (Exception ex) { returnResponse.ReturnStatus = false; returnResponse.ReturnMessage.Add(ex.Message); return(BadRequest(returnResponse)); } }
public async Task <IActionResult> GetById(int id) { _logger.LogInformation($"GetById Called {DateTimeOffset.UtcNow}"); EntityResponseModel <ApplicantModel> returnResponse = new EntityResponseModel <ApplicantModel>(); try { var result = await _IApplicantService.GetById(id); if (result.ReturnStatus == false) { return(BadRequest(JsonConvert.SerializeObject(result))); } return(Ok(JsonConvert.SerializeObject(result))); } catch (Exception ex) { returnResponse.ReturnStatus = false; returnResponse.ReturnMessage.Add(ex.Message); return(BadRequest(JsonConvert.SerializeObject(returnResponse))); } }
public async Task <IActionResult> Insert(ApplicantModel model) { _logger.LogInformation($"Isert Called {DateTimeOffset.UtcNow}"); var returnResponse = new EntityResponseModel <ApplicantModel>(); try { var result = await _IApplicantService.Insert(model); if (result.ReturnStatus == false) { return(StatusCode((int)HttpStatusCode.BadRequest, JsonConvert.SerializeObject(result))); } return(StatusCode((int)HttpStatusCode.Created, JsonConvert.SerializeObject(result))); } catch (Exception ex) { returnResponse.ReturnStatus = false; returnResponse.ReturnMessage.Add(ex.Message); return(BadRequest(JsonConvert.SerializeObject(returnResponse))); } }
public async Task <IActionResult> Insert(CancellationToken cancellationToken, [FromForm] ProductImageCreateDto model) { var returnResponse = new EntityResponseModel <ProductImageReadDto>(); try { var command = new EntityCreateCommand <ProductImageCreateDto, EntityResponseModel <ProductImageReadDto> >(model); var result = await Mediator.Send(command, cancellationToken).ConfigureAwait(false); if (result.ReturnStatus == false) { return(BadRequest(result)); } return(Ok(result)); } catch (Exception ex) { returnResponse.ReturnStatus = false; returnResponse.ReturnMessage.Add(ex.Message); return(BadRequest(returnResponse)); } }