Example #1
0
        public async Task PostNewPlantReturnsBadRequestAndCorrectContentType()
        {
            // Given
            CreatePlantViewModel model = ViewModelFactory.CreateInvalidCreationModel();

            // When
            HttpResponseMessage response = await Client.PostAsJsonAsync(EndPointFactory.CreateEndpoint(), model);

            // Then
            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
            response.Content.Headers.ContentType.ToString().Should().Be("application/problem+json; charset=utf-8");
        }
Example #2
0
        public async Task <IActionResult> Create(CreatePlantViewModel viewmodel)
        {
            ModelState.Remove("User");
            ModelState.Remove("UserId");


            //Attempting to use reflection as a means to change all plant types to value "n/a" if null.

            //PropertyInfo[] properties = viewmodel.plant.GetType().GetProperties();

            //foreach (var property in properties)
            //{
            //    if (property.PropertyType == typeof(string) && property.GetValue(viewmodel.plant) != null)
            //    {
            //        string newvalue = property.GetValue(this).ToString();
            //        property.SetValue(viewmodel.plant, "n/a");
            //    }
            //}

            if (ModelState.IsValid)
            {
                string uniqueFileName = null;

                // If the Photo property on the incoming model object is not null, then the user
                // has selected an image to upload.
                if (viewmodel.Photo != null)
                {
                    // The image must be uploaded to the images folder in wwwroot
                    // To get the path of the wwwroot folder we are using the inject
                    // HostingEnvironment service provided by ASP.NET Core
                    string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");
                    // To make sure the file name is unique we are appending a new
                    // GUID value and and an underscore to the file name
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + viewmodel.Photo.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    // Use CopyTo() method provided by IFormFile interface to
                    // copy the file to wwwroot/images folder
                    viewmodel.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }
                var plant    = viewmodel.plant;
                var currUser = await GetCurrentUserAsync();

                plant.ImagePath = uniqueFileName;
                plant.userId    = currUser.Id;
                _context.Add(plant);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewmodel));
        }
Example #3
0
        public async Task <Guid> CreatePlant(CreatePlantViewModel model)
        {
            var plant = new Plant()
            {
                Name                    = model.Name,
                ShortDescription        = model.ShortDescription,
                EstimatedTimeForGrowing = model.EstimatedTimeForGrowing,
            };

            await this._plantRepository.AddAsync(plant);

            await this._plantRepository.SaveChangesAsync();

            return(plant.Id);
        }
Example #4
0
        public async Task <ActionResult> Post([FromBody] CreatePlantViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                CreatePlantModel createPlantModel = _mapper.Map <CreatePlantModel>(model);
                Guid             plantId          = await _commands.Create(createPlantModel);

                return(CreatedAtRoute(nameof(GetPlant), new { id = plantId }, null));
            }
            catch (Exception ex) when(ex is ResourceNotFoundException)
            {
                return(NotFound(new ErrorViewModel(ex)));
            }
            catch (Exception ex) when(ex is ResourceStateException)
            {
                return(Conflict(new ErrorViewModel(ex)));
            }
        }
        public async Task <IActionResult> Create(CreatePlantViewModel model)
        {
            var id = await this._service.CreatePlant(model);

            return(this.RedirectToAction("Details", new { id = id }));
        }
        public IActionResult Create()
        {
            var plantViewModel = new CreatePlantViewModel();

            return(this.View(plantViewModel));
        }