public async Task <ProvinceResponse> DeleteAsync(int id)
        {
            var existingProvince = await _provinceRepository.FindById(id);

            if (existingProvince == null)
            {
                return(new ProvinceResponse("Province not found"));
            }
            try
            {
                IEnumerable <City> cities = await _cityRepository.ListByProvinceIdAsync(id);

                IEnumerable <OwnerLocation> ownerLocations = await _ownerLocationRepository.ListByProvinceIdAsync(id);

                cities.ToList().ForEach(city => {
                    _cityRepository.Remove(city);
                });
                ownerLocations.ToList().ForEach(ownerLocation => {
                    _ownerLocationRepository.Remove(ownerLocation);
                });

                _provinceRepository.Remove(existingProvince);
                await _unitOfWork.CompleteAsync();

                return(new ProvinceResponse(existingProvince));
            }
            catch (Exception ex)
            {
                return(new ProvinceResponse($"An error ocurred while deleting tag: {ex.Message}"));
            }
        }
        public async Task <PetProfileResponse> SaveAsync(int cityId, int provinceId, PetProfile petProfile)
        {
            var existingProvince = await _provinceRepository.FindById(provinceId);

            var existingCity = await _cityRepository.FindById(cityId);

            if (existingProvince == null)
            {
                return(new PetProfileResponse("Province not found, a profile needs a province to exist"));
            }
            if (existingCity == null)
            {
                return(new PetProfileResponse("City not found, a profile needs a city to exist"));
            }
            if (existingCity.ProvinceId != provinceId)
            {
                return(new PetProfileResponse("The City does not exist in the province"));
            }
            try
            {
                await _petProfileRepository.AddAsync(petProfile);

                await _unitOfWork.CompleteAsync();

                return(new PetProfileResponse(petProfile));
            }
            catch (Exception ex)
            {
                return(new PetProfileResponse($"An error ocurred while saving PetProfile: {ex.Message}"));
            }
        }
Example #3
0
        public async Task <VetProfileResponse> SaveAsync(int provinceId, int cityId, int userId, VetProfile vetProfile)
        {
            var existingProvince = await _provinceRepository.FindById(provinceId);

            var existingCity = await _cityRepository.FindById(cityId);

            var existingUser = await _userRepository.FindByIdAsync(userId);

            if (existingProvince == null)
            {
                return(new VetProfileResponse("Province not found, a vet needs a province to exist"));
            }
            if (existingCity == null)
            {
                return(new VetProfileResponse("City not found, a vet needs a city to exist"));
            }
            if (existingCity.ProvinceId != provinceId)
            {
                return(new VetProfileResponse("The City does not exist in the province"));
            }
            if (existingUser == null)
            {
                return(new VetProfileResponse("The User does not exist, a profile of vet depends of an user"));
            }
            IEnumerable <VetProfile> vetProfiles = await ListAsync();

            List <VetProfile> vetProfilesList = vetProfiles.ToList();
            bool differentUserId = true;

            vetProfilesList.ForEach(vetP =>
            {
                if (vetP.UserId == userId)
                {
                    differentUserId = false;
                }
            });
            if (!differentUserId)
            {
                return(new VetProfileResponse("The User is on used of other profile"));
            }
            if (existingUser.UserTypeVet == false)
            {
                return(new VetProfileResponse("The User is for owner profiles"));
            }
            try
            {
                await _vetProfileRepository.AddAsync(vetProfile);

                await _unitOfWork.CompleteAsync();

                return(new VetProfileResponse(vetProfile));
            }
            catch (Exception ex)
            {
                return(new VetProfileResponse($"An error ocurred while saving VetProfile: {ex.Message}"));
            }
        }
Example #4
0
        /// <summary>
        /// Get Address object and validate if department, province and district are valid
        /// </summary>
        /// <param name="idDepartment">Id Department</param>
        /// <param name="idDistrict">Id District</param>
        /// <param name="idProvince">Id Province</param>
        /// <returns>Address with names</returns>
        private Address GetAddressName(string idDepartment, string idDistrict, string idProvince)
        {
            var department = _departmentRepository.FindById(idDepartment);
            var district   = _districtRepository.FindById(idDistrict);
            var province   = _provinceRepository.FindById(idProvince);

            //if not found a department, then is invalid
            if (department == null)
            {
                throw new AppException("Department is not valid.");
            }
            //if not found a district, then is invalid
            if (district == null)
            {
                throw new AppException("District is not valid.");
            }
            //if not found a province, then is invalid
            if (province == null)
            {
                throw new AppException("Province is not valid.");
            }
            //Province doesn't belong department
            if (province.IdDepartment != department.Id)
            {
                throw new AppException("Province doesn't belong to selected department");
            }
            //District doesn't belong province
            if (province.Id != district.IdProvince)
            {
                throw new AppException("District doesn't belong to selected province");
            }

            return(new Address
            {
                Department = department.Name,
                District = district.Name,
                Province = province.Name
            });
        }
Example #5
0
        public async Task <CityResponse> SaveAsync(int provinceId, City city)
        {
            var existingProvince = await _provinceRepository.FindById(provinceId);

            if (existingProvince == null)
            {
                return(new CityResponse("Province not found, a city depent of a Province"));
            }
            try
            {
                city.ProvinceId = provinceId;

                await _cityRepository.AddAsync(city);

                await _unitOfWork.CompleteAsync();

                return(new CityResponse(city));
            }
            catch (Exception ex)
            {
                return(new CityResponse($"An error ocurred while saving city: {ex.Message}"));
            }
        }
Example #6
0
 // Province Business Logic
 public Provinces FindProvinceById(string id)
 {
     return(_provinceRepository.FindById(id));
 }
Example #7
0
 public ProvinceViewModel GetById(int id)
 {
     return(Mapper.Map <Province, ProvinceViewModel>(_provinceRepository.FindById(id)));
 }