public async Task <bool> DeleteServiceTermsAsync(cor_serviceterms model)
        {
            var itemToDelete = await _dataContext.cor_serviceterms.FindAsync(model.ServiceTermsId);

            if (itemToDelete != null)
            {
                _dataContext.cor_serviceterms.Remove(itemToDelete);
            }
            return(await _dataContext.SaveChangesAsync() > 0);
        }
        public async Task <bool> AddUpdateSeviceTermAsync(cor_serviceterms model)
        {
            if (model.ServiceTermsId > 0)
            {
                var itemToEdit = await _dataContext.cor_serviceterms.FindAsync(model.ServiceTermsId);

                _dataContext.Entry(itemToEdit).CurrentValues.SetValues(model);
            }
            else
            {
                await _dataContext.cor_serviceterms.AddAsync(model);
            }
            return(await _dataContext.SaveChangesAsync() > 0);
        }
            public async Task <FileUploadRespObj> Handle(UploadServiceTermsCommand 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 userId = _accessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value;
                    var user   = await _serverRequest.UserDataAsync();

                    var uploadedRecord = new List <ServiceTermObj>();
                    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 totalColumns = workSheet.Dimension.Columns;

                                    if (totalColumns != 2)
                                    {
                                        apiResponse.Status.Message.FriendlyMessage = "2 Columns Expected";
                                        return(apiResponse);
                                    }

                                    for (int i = 2; i <= totalRows; i++)
                                    {
                                        var item = new ServiceTermObj
                                        {
                                            ExcelLineNumber = i,
                                            Header          = workSheet.Cells[i, 1]?.Value != null ? workSheet.Cells[i, 1]?.Value.ToString() : string.Empty,
                                            Content         = workSheet.Cells[i, 2]?.Value != null ? workSheet.Cells[i, 2]?.Value.ToString() : string.Empty,
                                        };
                                        uploadedRecord.Add(item);
                                    }
                                }
                        }
                    }


                    var ItemList = await _repo.GetAllServiceTermsAsync();

                    cor_serviceterms itemFrmRepo = new cor_serviceterms();
                    if (uploadedRecord.Count > 0)
                    {
                        foreach (var item in uploadedRecord)
                        {
                            if (string.IsNullOrEmpty(item.Content))
                            {
                                apiResponse.Status.Message.FriendlyMessage = $" Empty Content Expected Detected on line {item.ExcelLineNumber}";
                                return(apiResponse);
                            }
                            if (string.IsNullOrEmpty(item.Header))
                            {
                                apiResponse.Status.Message.FriendlyMessage = $" Empty Header Expected Detected on line {item.ExcelLineNumber}";
                                return(apiResponse);
                            }

                            itemFrmRepo = ItemList.FirstOrDefault(c => c.Header.ToLower().Trim() == item.Header.ToLower().Trim());

                            if (itemFrmRepo != null)
                            {
                                itemFrmRepo.Header  = item?.Header;
                                itemFrmRepo.Content = item?.Content;

                                await _repo.AddUpdateSeviceTermAsync(itemFrmRepo);
                            }
                            else
                            {
                                var newItem = new cor_serviceterms
                                {
                                    Header  = item?.Header,
                                    Content = item?.Content,
                                };
                                await _repo.AddUpdateSeviceTermAsync(newItem);
                            }
                        }
                    }
                    apiResponse.Status.IsSuccessful            = true;
                    apiResponse.Status.Message.FriendlyMessage = "Successful";
                    return(apiResponse);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
Exemplo n.º 4
0
        public async Task <ServiceTermRegRespObj> Handle(AddUpdateServiceTermCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var user = await _serverRequest.UserDataAsync();

                if (user == null)
                {
                    return(new ServiceTermRegRespObj
                    {
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = true,
                            Message = new APIResponseMessage
                            {
                                FriendlyMessage = $"Unable to process user details",
                            }
                        }
                    });
                }

                var data = _dataContext.cor_serviceterms.Where(w => w.Header.Trim().ToLower() == request.Header.Trim().ToLower() && request.ServiceTermsId != w.ServiceTermsId).ToList();

                if (data.Count() > 1)
                {
                    return(new ServiceTermRegRespObj
                    {
                        Status = new APIResponseStatus
                        {
                            IsSuccessful = false,
                            Message = new APIResponseMessage
                            {
                                FriendlyMessage = $"Header Already exist",
                            }
                        }
                    });
                }

                cor_serviceterms sup = new cor_serviceterms();
                sup.Deleted   = true;
                sup.CreatedOn = request.ServiceTermsId > 0 ? (DateTime?)null : DateTime.Now;
                sup.CreatedBy = user.UserName;
                sup.UpdatedBy = user.UserName;
                sup.Header    = request.Header;
                sup.Content   = request.Content;
                sup.Deleted   = false;
                if (request.ServiceTermsId > 0)
                {
                    sup.ServiceTermsId = request.ServiceTermsId;
                }
                await _supRepo.AddUpdateSeviceTermAsync(sup);

                var actionTaken = request.ServiceTermsId < 1 ? "created" : "updated";
                return(new ServiceTermRegRespObj
                {
                    ServiceTermId = sup.ServiceTermsId,
                    Status = new APIResponseStatus
                    {
                        IsSuccessful = true,
                        Message = new APIResponseMessage
                        {
                            FriendlyMessage = $"Successfully  {actionTaken}",
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                #region Log error to file
                var errorCode = ErrorID.Generate(4);
                _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}");
                return(new ServiceTermRegRespObj
                {
                    Status = new APIResponseStatus
                    {
                        IsSuccessful = false,
                        Message = new APIResponseMessage
                        {
                            FriendlyMessage = "Error occured!! Unable to process request",
                            MessageId = errorCode,
                            TechnicalMessage = $"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"
                        }
                    }
                });

                #endregion
            }
        }