Beispiel #1
0
        public async Task <bool> UpdateFarmerAsync(int id, FarmerModelRq model, UserIdentity <int> issuer)
        {
            try
            {
                var entity = _mapper.Map <Farmer>(model);
                entity = await _FarmerRepository.GetEntityByIdAsync(id);

                if (entity == null)
                {
                    return(false);
                }
                _mapper.Map(model, entity);
                entity.UpdateBy(issuer);
                _FarmerRepository.Update(entity);
                if (await _uow.SaveChangesAsync() == 1)
                {
                    return(true);
                }
                return(false);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            if (request.Farmer == null || request.Farmer.Id == 0 || request.Farmer.Address == null || request.Farmer.Contact == null)
            {
                throw new BusinessException("Farmer.NotExisted");
            }

            var farmer = await farmerQueries.Get(request.Farmer.Id);

            if (farmer == null)
            {
                throw new BusinessException("Farmer.NotExisted");
            }

            string oldImageUrl = request.Farmer.ImageURL;

            //With ImageData < 100byte. This is a link image. With Image > 100byte, It can a real imageData.
            if (request.Farmer.ImageData?.Length > 100)
            {
                string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Farmer.ImageData));
                if (!CommonHelper.IsImageType(type))
                {
                    throw new BusinessException("Image.WrongType");
                }
                string Base64StringData = request.Farmer.ImageData.Substring(request.Farmer.ImageData.IndexOf(",") + 1);
                string fileName         = Guid.NewGuid().ToString().Replace("-", "");
                request.Farmer.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.FarmerImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData);
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        farmerRepository.JoinTransaction(conn, trans);
                        locationRepository.JoinTransaction(conn, trans);

                        farmer.Address            = farmer.Address ?? new Address();
                        farmer.Address.Street     = request.Farmer.Address.Street;
                        farmer.Address.CountryId  = request.Farmer.Address.CountryId;
                        farmer.Address.ProvinceId = request.Farmer.Address.ProvinceId;
                        farmer.Address.DistrictId = request.Farmer.Address.DistrictId;
                        farmer.Address.WardId     = request.Farmer.Address.WardId;
                        farmer.Address.Longitude  = request.Farmer.Address.Longitude;
                        farmer.Address.Latitude   = request.Farmer.Address.Latitude;
                        farmer.Address            = UpdateBuild(farmer.Address, request.LoginSession);
                        if (await locationRepository.AddOrUpdateAddress(farmer.Address) == -1)
                        {
                            return(rs = -1);
                        }

                        farmer.Contact        = farmer.Contact ?? new Contact();
                        farmer.Contact.Name   = request.Farmer.Contact.Name;
                        farmer.Contact.Phone  = request.Farmer.Contact.Phone;
                        farmer.Contact.Email  = request.Farmer.Contact.Email;
                        farmer.Contact.Gender = request.Farmer.Contact.Gender;
                        farmer.Contact        = UpdateBuild(farmer.Contact, request.LoginSession);
                        if (await locationRepository.AddOrUpdateContact(farmer.Contact) == -1)
                        {
                            return(rs = -1);
                        }

                        farmer           = UpdateBuild(farmer, request.LoginSession);
                        farmer.ImageURL  = request.Farmer.ImageURL;
                        farmer.Name      = request.Farmer.Name;
                        farmer.UserId    = request.Farmer.UserId;
                        farmer.IsUsed    = request.Farmer.IsUsed;
                        farmer.IsCompany = request.Farmer.IsCompany;

                        if (string.IsNullOrWhiteSpace(farmer.Code))
                        {
                            farmer.Code = (await storageQueries.GenarateCodeAsync(StorageKeys.FarmerCode));
                        }

                        if (!request.Farmer.IsCompany)
                        {
                            farmer.TaxCode = string.Empty;
                        }
                        else
                        {
                            farmer.TaxCode = request.Farmer.TaxCode;
                        }
                        rs = await farmerRepository.Update(farmer);
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                            CommonHelper.DeleteImage(oldImageUrl);
                        }
                        else
                        {
                            try
                            {
                                trans.Commit();
                            }
                            catch { }
                            CommonHelper.DeleteImage(request.Farmer.ImageURL);
                        }
                    }
                }
            }

            return(rs);
        }
        public override async Task <int> HandleCommand(UpdateProfileCommand request, CancellationToken cancellationToken)
        {
            if (request.Farmer == null || request.Farmer.FarmerId == 0)
            {
                throw new BusinessException("Farmer.NotExisted");
            }

            var farmer = await farmerQueries.Get(request.Farmer.FarmerId);

            if (farmer == null)
            {
                throw new BusinessException("Farmer.NotExisted");
            }

            var farmerSession = await farmerQueries.GetByUser(request.LoginSession.Id);

            if (farmerSession == null || farmerSession.Id == 0 || farmerSession.Id != farmer.Id)
            {
                throw new NotPermissionException();
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        farmerRepository.JoinTransaction(conn, trans);
                        locationRepository.JoinTransaction(conn, trans);

                        farmer.Address            = farmer.Address ?? new Address();
                        farmer.Address.Street     = request.Farmer.Street;
                        farmer.Address.CountryId  = request.Farmer.CountryId;
                        farmer.Address.ProvinceId = request.Farmer.ProvinceId;
                        farmer.Address.DistrictId = request.Farmer.DistrictId;
                        farmer.Address.WardId     = request.Farmer.WardId;
                        //farmer.Address.Longitude = request.Farmer.Longitude;
                        //farmer.Address.Latitude = request.Farmer.Latitude;
                        farmer.Address = UpdateBuild(farmer.Address, request.LoginSession);
                        if (await locationRepository.AddOrUpdateAddress(farmer.Address) == -1)
                        {
                            return(rs = -1);
                        }

                        farmer.Contact        = farmer.Contact ?? new Contact();
                        farmer.Contact.Name   = request.Farmer.Name;
                        farmer.Contact.Phone  = request.Farmer.Phone;
                        farmer.Contact.Email  = request.Farmer.Email;
                        farmer.Contact.Gender = request.Farmer.Gender;
                        farmer.Contact        = UpdateBuild(farmer.Contact, request.LoginSession);

                        if (await locationRepository.AddOrUpdateContact(farmer.Contact) == -1)
                        {
                            return(rs = -1);
                        }

                        farmer      = UpdateBuild(farmer, request.LoginSession);
                        farmer.Name = request.Farmer.Name;
                        if (string.IsNullOrWhiteSpace(farmer.Code))
                        {
                            farmer.Code = (await storageQueries.GenarateCodeAsync(StorageKeys.FarmerCode));
                        }

                        rs = await farmerRepository.Update(farmer);
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Commit();
                            }
                            catch { }
                        }
                    }
                }
            }

            return(rs);
        }
        public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken)
        {
            if (request.Farmer == null || request.Farmer.Address == null || request.Farmer.Contact == null)
            {
                throw new BusinessException("AddWrongInformation");
            }

            if (request.Farmer.ImageData?.Length > Constant.MaxImageLength)
            {
                throw new BusinessException("Image.OutOfLength");
            }
            //With ImageData < 100byte. This is a link image. With Image > 100byte, It can a real imageData.
            if (request.Farmer.ImageData?.Length > 100)
            {
                string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Farmer.ImageData));
                if (!CommonHelper.IsImageType(type))
                {
                    throw new BusinessException("Image.WrongType");
                }
                string Base64StringData = request.Farmer.ImageData.Substring(request.Farmer.ImageData.IndexOf(",") + 1);
                string fileName         = Guid.NewGuid().ToString().Replace("-", "");
                request.Farmer.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.FarmerImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData);
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        farmerRepository.JoinTransaction(conn, trans);
                        locationRepository.JoinTransaction(conn, trans);

                        request.Farmer = CreateBuild(request.Farmer, request.LoginSession);
                        if (!request.Farmer.IsCompany)
                        {
                            request.Farmer.TaxCode = string.Empty;
                        }
                        request.Farmer.Code = (await storageQueries.GenarateCodeAsync(StorageKeys.FarmerCode));

                        request.Farmer.Id = await farmerRepository.Add(request.Farmer);

                        request.Farmer.Address.Id         = 0;
                        request.Farmer.Address.ObjectType = LocationOjectType.F.ToString();
                        request.Farmer.Address.ObjectId   = request.Farmer.Id;
                        request.Farmer.Address.IsUsed     = true;
                        request.Farmer.Address            = CreateBuild(request.Farmer.Address, request.LoginSession);
                        var addressId = await locationRepository.AddOrUpdateAddress(request.Farmer.Address);

                        request.Farmer.Contact.Id         = 0;
                        request.Farmer.Contact.ObjectType = LocationOjectType.F.ToString();
                        request.Farmer.Contact.ObjectId   = request.Farmer.Id;
                        request.Farmer.Contact.IsUsed     = true;
                        request.Farmer.Contact            = CreateBuild(request.Farmer.Contact, request.LoginSession);
                        var contactId = await locationRepository.AddOrUpdateContact(request.Farmer.Contact);

                        request.Farmer.AddressId = addressId;
                        request.Farmer.ContactId = contactId;


                        rs = await farmerRepository.Update(request.Farmer);
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Commit();
                            }
                            catch { }
                            CommonHelper.DeleteImage(request.Farmer.ImageURL);
                        }
                    }
                }
            };

            return(rs == 0 ? -1 : 0);
        }