public async Task <bool> CreateAsync(CreatePropertyModel model)
        {
            var company = await this.Db.Companies
                          .FirstOrDefaultAsync(x => x.Id == model.CompanyId);

            if (company == null)
            {
                return(false);
            }
            var property = new Property
            {
                Address     = model.PropertyAddress,
                Name        = model.PropertyName,
                Area        = model.Area,
                Description = model.Description,
                Type        = model.Type,
                CompanyId   = company.Id
            };

            await this.Db.Properties.AddAsync(property);

            try
            {
                await Db.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
        public async Task CreateAsync_SouldReturn_True_IfInputDataIsCorrectFromDb()
        {
            var db     = this.GetDatabase();
            var mapper = this.GetMapper();


            var firstProp = new Property
            {
                Id       = 3,
                Name     = "Голям склад",
                Area     = 400,
                IsActual = true
            };
            var companyOne = new Company
            {
                Id         = 1,
                Name       = "First",
                Properties = new List <Property>()
                {
                    firstProp
                }
            };
            await db.Companies.AddRangeAsync(companyOne);

            await db.SaveChangesAsync();

            var propertyService = new PropertiesService(mapper, db);
            var model           = new CreatePropertyModel
            {
                CompanyId       = 1,
                PropertyName    = "Office",
                Description     = "No Descriprion",
                Area            = 80,
                Type            = PropertyType.Office,
                PropertyAddress = "Mars"
            };

            //Act
            var result = await propertyService.CreateAsync(model);

            var addedProperty = await db.Properties.FirstOrDefaultAsync(x => x.Name == "Office");

            //Assert
            result
            .Should()
            .BeTrue();

            addedProperty
            .Should()
            .NotBeNull()
            .And
            .BeOfType <Property>()
            .And
            .Match <Property>(p => p.Type == PropertyType.Office &&
                              p.Address == "Mars" &&
                              p.Area == 80 &&
                              p.CompanyId == 1);
        }
        public async Task CreateAsync_SouldReturn_False_IfCompanyDoNotExistsDb()
        {
            var db     = this.GetDatabase();
            var mapper = this.GetMapper();

            var firstProp = new Property
            {
                Id       = 3,
                Name     = "Голям склад",
                Area     = 400,
                IsActual = true
            };
            var companyOne = new Company
            {
                Id         = 1,
                Name       = "First",
                Properties = new List <Property>()
                {
                    firstProp
                }
            };
            await db.Companies.AddRangeAsync(companyOne);

            await db.SaveChangesAsync();

            var propertyService = new PropertiesService(mapper, db);

            var model = new CreatePropertyModel
            {
                CompanyId       = 2, // ID do not exsist
                PropertyName    = "Office",
                Description     = "No Descriprion",
                Area            = 80,
                Type            = PropertyType.Office,
                PropertyAddress = "Mars"
            };

            //Act
            var result = await propertyService.CreateAsync(model);

            var addedProperty = await db.Properties.FirstOrDefaultAsync(x => x.Name == "Office");

            //Assert
            result
            .Should()
            .BeFalse();

            addedProperty
            .Should()
            .BeNull();
        }
Exemplo n.º 4
0
        // POST: Property/Create
        public ActionResult Create(CreatePropertyModel model)
        {
            if (ModelState.IsValid)
            {
                var location = Mapper.Map <Location>(model);

                var propertyId = _property.Create(UserId(), location, model.Description);

                return(RedirectToAction("Edit", new { propertyId = propertyId }));
            }

            ViewBag.ModelValidationError = Zen.Imobi.Resources.Property.ViewCreate_ModelValidationError;

            return(View(model));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create(CreatePropertyModel model)
        {
            if (!ModelState.IsValid)
            {
                TempData.AddErrorMessage(WrongInput);
                return(this.Redirect("/Properties/Index"));
            }

            var isCreated = await this.properties.CreateAsync(model);

            if (!isCreated)
            {
                return(BadRequest());
            }

            TempData.AddSuccessMessage("Новият имот беше успешно създаден!");
            return(this.Redirect("/Properties/Index"));
        }
Exemplo n.º 6
0
        public ResponseWrapper <CreatePropertyModel> CreateProperty(CreatePropertyInputModel model)
        {
            var newEntity = new Property
            {
                Name         = model.Name,
                PropertyType = model.PropertyType,
                EntityId     = model.EntityId,
            };

            context
            .Properties
            .Add(newEntity);

            context.SaveChanges();
            var response = new CreatePropertyModel
            {
                PropertyId   = newEntity.PropertyId,
                Name         = newEntity.Name,
                PropertyType = newEntity.PropertyType,
                EntityId     = newEntity.EntityId,
            };

            return(new ResponseWrapper <CreatePropertyModel>(_validationDictionary, response));
        }