public static void AddAddresses(StudioDbContext context)
        {
            InputAddressData addressData = new InputAddressData
            {
                Street   = "Vasil Levski",
                Number   = "10",
                Building = "3G",
                Entrance = "B"
            };

            var cityId = CommandArrangeHelper.GetCityId(context, null);

            var addresses = new List <Address>
            {
                new Address {
                    Id = 1, AddressFormat = (AddressFormat)addressData, Longitude = 40.35M, Latitude = 56.23M, CityId = cityId
                },
                new Address {
                    Id = 2, AddressFormat = (AddressFormat)addressData, Longitude = 40.35M, Latitude = 56.23M, CityId = cityId
                },
                new Address {
                    Id = 3, AddressFormat = (AddressFormat)addressData, Longitude = 40.35M, Latitude = 56.23M, CityId = cityId
                }
            };

            context.Addresses.AddRange(addresses);
            context.SaveChanges();
        }
        public static int GetAddressId(StudioDbContext context, int?cityId)
        {
            var inputAddressData = new InputAddressData
            {
                Street = GConst.ValidName,
                Number = GConst.ValidAddressNumber
            };

            Address address = new Address {
                AddressFormat = AddressFormat.For(inputAddressData)
            };

            if (cityId != null)
            {
                address = new Address {
                    AddressFormat = AddressFormat.For(inputAddressData), CityId = cityId.Value
                };
            }

            context.Addresses.Add(address);
            context.SaveChangesAsync();

            var addressId = context.Addresses.SingleOrDefault(x => x.AddressFormat.Street == GConst.ValidName).Id;

            return(addressId);
        }
Ejemplo n.º 3
0
        public async Task <Unit> Handle(UpdateAddressCommand request, CancellationToken cancellationToken)
        {
            var address = await this.context.Addresses
                          .SingleOrDefaultAsync(a => a.Id == request.Id && a.IsDeleted != true, cancellationToken);

            if (address == null)
            {
                throw new NotFoundException(GConst.Address, request.Id);
            }

            var city = await this.context.Cities.FindAsync(request.CityId);

            if (city == null || city.IsDeleted == true)
            {
                throw new UpdateFailureException(GConst.Address, request.Street, string.Format(GConst.RefereceException, GConst.CityLower, request.CityId));
            }

            var inputAddressData = new InputAddressData
            {
                Street    = request.Street,
                Number    = request.Number,
                Building  = request.Building,
                Entrance  = request.Entrance,
                Floor     = request.Floor,
                Apartment = request.Apartment,
                District  = request.District
            };

            address.AddressFormat = AddressFormat.For(inputAddressData);
            address.Longitude     = request.Longitude;
            address.Latitude      = request.Latitude;
            address.CityId        = request.CityId;
            address.ModifiedOn    = DateTime.UtcNow;

            this.context.Addresses.Update(address);
            await this.context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 4
0
        public async Task <Unit> Handle(CreateAddressCommand request, CancellationToken cancellationToken)
        {
            var city = await this.context.Cities.FindAsync(request.CityId);

            if (city == null || city.IsDeleted == true)
            {
                throw new CreateFailureException(GConst.Address, request.Street, string.Format(GConst.RefereceException, GConst.CityLower, request.CityId));
            }

            var inputAddressData = new InputAddressData
            {
                Street    = request.Street,
                Number    = request.Number,
                Building  = request.Building,
                Entrance  = request.Entrance,
                Floor     = request.Floor,
                Apartment = request.Apartment,
                District  = request.District
            };

            var address = new Address
            {
                AddressFormat = AddressFormat.For(inputAddressData),
                Latitude      = request.Latitude,
                Longitude     = request.Longitude,
                CityId        = request.CityId,
                CreatedOn     = DateTime.UtcNow,
                IsDeleted     = false
            };

            this.context.Addresses.Add(address);

            await this.context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }