Example #1
0
        public async Task <IActionResult> Put(int id, [FromBody] StationCreateUpdateDto update)
        {
            var result = await this.businessLayer.UpdateStationAsync(update, id);

            return(result.Match <IActionResult>(
                       success => Ok(result.RightValue),
                       failure => failure.ToActionResult()
                       ));
        }
        public void ShouldMapStationCreateUpdateDtoToStation(StationCreateUpdateDto station)
        {
            var mapped = this.mapper.Map <Station>(station);

            Assert.Null(mapped.StationId);
            Assert.Equal(station.Title, mapped.Title);
            Assert.Equal(station.Url, mapped.Url);
            Assert.Equal(station.IconUrl, mapped.IconUrl);
            Assert.Equal(station.Notes, mapped.Notes);
        }
Example #3
0
        public async Task <StationDto> CreateStationAsync(StationCreateUpdateDto station)
        {
            // Map the DTO to an entity model and add the current user's id
            var mapped = this.mapper.Map <Station>(station);

            mapped.UserId = this.user.UniqueId;
            // Create the new station in the DB
            var newStation = await this.dataLayer.CreateStationAsync(mapped);

            // Map the created station to a DTO and return it
            return(this.mapper.Map <StationDto>(newStation));
        }
        public async void ShouldApplyTheCurrentUserIdToNewStations(UserStub user)
        {
            // Arrange: Init the businesslayer with the provided user & create a dummy StationDTO
            this.businessLayer = new BusinessLayer(this.dataLayer, user, this.mapper);
            var newStation = new StationCreateUpdateDto()
            {
                Title = "New Station"
            };

            // Act: Create the station
            await this.businessLayer.CreateStationAsync(newStation);

            // Assert that the station was created with the provided UserId
            await this.dataLayer.Received(1).CreateStationAsync(Arg.Is <Station>(s => s.UserId == user.UniqueId));
        }
        public async void UpdateStationAsyncShouldProperlyUpdateTheStation()
        {
            // Arrange
            // Init a dummy original station and return it when GetStationAsync is called
            Station originalStation = new Station()
            {
                StationId = 1,
                UserId    = "Dummy Test User",
                Title     = "Original Title",
                Url       = "Original URL",
                IconUrl   = "Original Icon URL",
                Notes     = "Original Notes"
            };

            this.dataLayer.GetStationAsync(1).Returns(originalStation);
            // Init a dummy station update
            StationCreateUpdateDto newStation = new StationCreateUpdateDto()
            {
                Title   = "New Title",
                Url     = "New URL",
                IconUrl = "New Icon URL",
                Notes   = "New Notes"
            };

            // Act: Update the station
            var result = this.businessLayer.UpdateStationAsync(newStation, 1);

            /* Assert that the updated station model passed to the data layer
             * has the original, unmodified StationId and UserId, along with the new
             * values for all of the other properties. */
            await this.dataLayer.Received(1).UpdateStationAsync(Arg.Is <Station>(u =>
                                                                                 u.StationId == originalStation.StationId &&
                                                                                 u.UserId == originalStation.UserId &&
                                                                                 u.Title == newStation.Title &&
                                                                                 u.Url == newStation.Url &&
                                                                                 u.IconUrl == newStation.IconUrl &&
                                                                                 u.Notes == newStation.Notes
                                                                                 ));
        }
Example #6
0
        public async Task <Either <BusinessOperationFailureReason, StationDto> > UpdateStationAsync(StationCreateUpdateDto station, int stationId)
        {
            // Retrieve the station from the database
            var existing = await this.dataLayer.GetStationAsync(stationId);

            // Check to see if the station wasn't found or is associated with a different user
            if (existing == null)
            {
                return(Either.Left(BusinessOperationFailureReason.ResourceDoesNotExist));
            }
            else if (existing.UserId != this.user.UniqueId)
            {
                return(Either.Left(BusinessOperationFailureReason.ResourceDoesNotBelongToCurrentUser));
            }
            // Apply the values from the provided DTO to the existing entity model
            this.mapper.Map <StationCreateUpdateDto, Station>(station, existing);
            // Persist the updated entity model to the DB
            var updated = await this.dataLayer.UpdateStationAsync(existing);

            // Map and return the updated station model
            return(Either.Right(this.mapper.Map <StationDto>(updated)));
        }
Example #7
0
        public async Task <IActionResult> Post([FromBody] StationCreateUpdateDto create)
        {
            var created = await this.businessLayer.CreateStationAsync(create);

            return(Created($"UserStations/{created.StationId}", created));
        }