Exemple #1
0
        public async Task <string> UpdateProfileAvatarAsync(string mobile, IFormFile photo)
        {
            var person = await _dbContext.Persons.FirstOrDefaultAsync(x => x.Mobile == mobile);

            if (person == null)
            {
                throw new AwroNoreException(NewResource.UserNotFound);
            }

            var strategy = _dbContext.Database.CreateExecutionStrategy();

            await strategy.ExecuteAsync(async() =>
            {
                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    var(newName, thumbName, dirPath, baseUrl) = _uploadService.GeneratePersonAvatarName(person.Id, photo);

                    person.Avatar = $"{baseUrl}/{thumbName}";

                    _dbContext.Persons.Attach(person);

                    _dbContext.Entry(person).State = EntityState.Modified;

                    await _dbContext.SaveChangesAsync();

                    await _uploadService.UploadPersonAvatarAsync(photo, dirPath, newName, thumbName);

                    transaction.Commit();
                }
            });

            return(person.RealAvatar);
        }
        public async Task <IActionResult> CreateUpdate(PersonCreateUpdateViewModel model)
        {
            if (string.IsNullOrEmpty(model.Mobile) || model.Mobile.Length < 10)
            {
                throw new Exception("Mobile is not valid");
            }

            var isNumeric = double.TryParse(model.Mobile, out _);

            if (!isNumeric)
            {
                throw new Exception("Mobile Number Is Not Valid");
            }

            if (model.Mobile.Length > 10)
            {
                model.Mobile = model.Mobile.Substring(model.Mobile.Length - 10);
            }

            var duplicateMobile = await _dbContext.Persons.AnyAsync(x => x.Id != model.Id && x.Mobile == model.Mobile);

            if (duplicateMobile)
            {
                throw new AwroNoreException(Core.Resources.EntitiesResources.Messages.PersonMobileDuplicate);
            }

            var success = false;
            var message = Core.Resources.Global.Global.Err_ErrorOccured;

            var strategy = _dbContext.Database.CreateExecutionStrategy();

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

                        if (person == null)
                        {
                            throw new Exception("Person Not Found");
                        }

                        person.FirstName     = model.FirstName;
                        person.FirstName_Ku  = model.FirstName_Ku;
                        person.FirstName_Ar  = model.FirstName_Ar;
                        person.SecondName    = model.SecondName;
                        person.SecondName_Ku = model.SecondName_Ku;
                        person.SecondName_Ar = model.SecondName_Ar;
                        person.ThirdName     = model.ThirdName;
                        person.ThirdName_Ku  = model.ThirdName_Ku;
                        person.ThirdName_Ar  = model.ThirdName_Ar;
                        person.Gender        = model.Gender;
                        person.Mobile        = model.Mobile;
                        person.IsEmployee    = model.IsEmployee;

                        _dbContext.Persons.Attach(person);

                        _dbContext.Entry(person).State = EntityState.Modified;

                        await _dbContext.SaveChangesAsync();

                        if (model.ImageUpload != null)
                        {
                            var(newName, thumbName, dirPath, baseUrl) = _uploadService.GeneratePersonAvatarName(person.Id, model.ImageUpload);

                            person.Avatar = $"{baseUrl}/{thumbName}";

                            _dbContext.Persons.Attach(person);

                            _dbContext.Entry(person).State = EntityState.Modified;

                            await _dbContext.SaveChangesAsync();

                            await _uploadService.UploadPersonAvatarAsync(model.ImageUpload, dirPath, newName, thumbName);
                        }

                        message = AN.Core.Resources.EntitiesResources.Messages.ItemUpdatedSuccessFully;
                    }
                    else
                    {
                        var person = _mapper.Map <Person>(model);

                        person.UniqueId = await _personService.GenerateUniqueIdAsync();

                        await _dbContext.Persons.AddAsync(person);

                        await _dbContext.SaveChangesAsync();

                        var(newName, thumbName, dirPath, baseUrl) = _uploadService.GeneratePersonAvatarName(person.Id, model.ImageUpload);

                        person.Avatar = $"{baseUrl}/{thumbName}";

                        _dbContext.Persons.Attach(person);

                        _dbContext.Entry(person).State = EntityState.Modified;

                        await _dbContext.SaveChangesAsync();

                        await _uploadService.UploadPersonAvatarAsync(model.ImageUpload, dirPath, newName, thumbName);

                        message = Core.Resources.EntitiesResources.Messages.ItemAddedSuccessFully;
                    }

                    transaction.Commit();

                    success = true;
                }
            });

            return(Json(new { success, message }));
        }