Exemplo n.º 1
0
        public async Task <int> CreateUpdateInsuranceServiceAsync(InsuranceServiceCRUDViewModel model)
        {
            var strategy = _dbContext.Database.CreateExecutionStrategy();

            await strategy.ExecuteAsync(async() =>
            {
                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    if (model.Id != null)
                    {
                        var service = await GetByIdAsync(model.Id.Value);

                        if (service == null)
                        {
                            throw new Exception("Insurance Service Not Found");
                        }

                        service.Title          = model.Title;
                        service.Title_Ar       = model.Title_Ar;
                        service.Title_Ku       = model.Title_Ku;
                        service.Summary        = model.Summary;
                        service.Summary_Ar     = model.Summary_Ar;
                        service.Summary_Ku     = model.Summary_Ku;
                        service.Description    = model.Description;
                        service.Description_Ar = model.Description_Ar;
                        service.Description_Ku = model.Description_Ku;
                        service.InsuranceId    = model.InsuranceId;
                        service.UpdatedAt      = DateTime.Now;

                        await UpdateAsync(service);

                        if (model.Photo != null)
                        {
                            var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateInsuranceServicePhotoName(service.Id, model.Photo);

                            service.Photo = $"{baseUrl}/{newName}";

                            await UpdateAsync(service);

                            await _uploadService.UploadInsuranceServicePhotoAsync(model.Photo, dirPath, newName, thumbName);
                        }
                    }
                    else
                    {
                        var insuranceService = new InsuranceService
                        {
                            Title          = model.Title,
                            Title_Ar       = model.Title_Ar,
                            Title_Ku       = model.Title_Ku,
                            Summary        = model.Summary,
                            Summary_Ar     = model.Summary_Ar,
                            Summary_Ku     = model.Summary_Ku,
                            Description    = model.Description,
                            Description_Ar = model.Description_Ar,
                            Description_Ku = model.Description_Ku,
                            HasAttachments = false,
                            InsuranceId    = model.InsuranceId,
                            CreatedAt      = DateTime.Now,
                        };

                        await InsertAsync(insuranceService);

                        if (model.Photo != null)
                        {
                            var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateInsuranceServicePhotoName(insuranceService.Id, model.Photo);

                            insuranceService.Photo = $"{baseUrl}/{newName}";

                            await UpdateAsync(insuranceService);

                            await _uploadService.UploadInsuranceServicePhotoAsync(model.Photo, dirPath, newName, thumbName);
                        }

                        model.Id = insuranceService.Id;
                    }

                    transaction.Commit();
                }
            });

            return(model.Id.Value);
        }