public async Task <LookUpRegRespObj> Handle(AddUpdateCityCommand request, CancellationToken cancellationToken) { try { if (request.CityId < 1) { if (await _dataContext.cor_jobtitles.AnyAsync(x => x.Name.Trim().ToLower() == request.CityName.Trim().ToLower() && x.Deleted == false)) { return(new LookUpRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "This Name Already Exist" } } }); } } var item = new cor_city { Active = true, CityId = request.CityId, CityCode = request.CityCode, CityName = request.CityName, StateId = request.StateId, CreatedOn = request.CreatedOn, Deleted = false, CreatedBy = request.CreatedBy }; var addedOrUpdated = await _repo.AddUpdateCityAsync(item); return(new LookUpRegRespObj { Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = "Successful" } } }); } catch (Exception ex) { #region Log error to file return(new LookUpRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Unable to process request", TechnicalMessage = $"{ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } }
public async Task <bool> AddUpdateCityAsync(cor_city model) { if (model.CityId > 0) { var item = await _dataContext.cor_city.FindAsync(model.CityId); _dataContext.Entry(item).CurrentValues.SetValues(model); } else { await _dataContext.cor_city.AddAsync(model); } return(await _dataContext.SaveChangesAsync() > 0); }
public async Task <FileUploadRespObj> Handle(UploadCityCommand request, CancellationToken cancellationToken) { try { var apiResponse = new FileUploadRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage() } }; var files = _accessor.HttpContext.Request.Form.Files; var byteList = new List <byte[]>(); foreach (var fileBit in files) { if (fileBit.Length > 0) { using (var ms = new MemoryStream()) { await fileBit.CopyToAsync(ms); byteList.Add(ms.ToArray()); } } } var uploadedRecord = new List <CommonLookupsObj>(); ExcelPackage.LicenseContext = LicenseContext.NonCommercial; if (byteList.Count() > 0) { foreach (var byteItem in byteList) { using (MemoryStream stream = new MemoryStream(byteItem)) using (ExcelPackage excelPackage = new ExcelPackage(stream)) { ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[0]; int totalRows = workSheet.Dimension.Rows; int totatlColumns = workSheet.Dimension.Columns; if (totatlColumns != 3) { apiResponse.Status.Message.FriendlyMessage = "Three (3) Columns Expected"; return(apiResponse); } for (int i = 2; i <= totalRows; i++) { var lkp = new CommonLookupsObj { ExcelLineNumber = i, Code = workSheet.Cells[i, 1]?.Value != null ? workSheet.Cells[i, 1]?.Value.ToString() : string.Empty, LookupName = workSheet.Cells[i, 2]?.Value != null ? workSheet.Cells[i, 2]?.Value.ToString() : string.Empty, ParentName = workSheet.Cells[i, 3]?.Value != null ? workSheet.Cells[i, 3]?.Value.ToString() : string.Empty, }; uploadedRecord.Add(lkp); } } } } var _CityList = await _repo.GetAllCityAsync(); var _State = await _repo.GetAllStateAsync(); if (uploadedRecord.Count > 0) { foreach (var item in uploadedRecord) { if (string.IsNullOrEmpty(item.Code)) { apiResponse.Status.Message.FriendlyMessage = $"City Code can not be empty on line {item.ExcelLineNumber}"; return(apiResponse); } if (string.IsNullOrEmpty(item.LookupName)) { apiResponse.Status.Message.FriendlyMessage = $"City Name can not be empty on line {item.ExcelLineNumber}"; return(apiResponse); } if (string.IsNullOrEmpty(item.ParentName)) { apiResponse.Status.Message.FriendlyMessage = $"State Name can not be empty on line {item.ExcelLineNumber}"; return(apiResponse); } else { var state = _State.FirstOrDefault(d => d.StateCode.Trim().ToLower() == item.ParentName.Trim().ToLower()); if (state == null) { apiResponse.Status.Message.FriendlyMessage = $"State name unidentified on line {item.ExcelLineNumber}"; return(apiResponse); } item.ParentId = state.StateId; } var currentCity = _CityList.FirstOrDefault(c => c.CityCode.ToLower() == item.Code.ToLower() && c.Deleted == false); if (currentCity == null) { var City = new cor_city(); City.CityName = item.LookupName; City.StateId = item.ParentId; City.CityCode = item.Code; await _repo.AddUpdateCityAsync(City); } else { currentCity.CityName = item.LookupName; currentCity.StateId = item.ParentId; currentCity.CityCode = item.Code; await _repo.AddUpdateCityAsync(currentCity); } } } apiResponse.Status.IsSuccessful = true; apiResponse.Status.Message.FriendlyMessage = "Successful"; return(apiResponse); } catch (Exception ex) { throw new Exception(ex.Message); } }