public async Task <APIResult> GetByUser(int?userId)
 {
     return(new APIResult()
     {
         Result = 0,
         Data = await farmerQueries.GetByUser(userId ?? LoginSession.Id)
     });
 }
        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);
        }