Exemplo n.º 1
0
        public async Task Should_Update_Parkinglot_Capacity_Success()
        {
            // Given
            var client = GetClient();

            ParkingLotDto parkingLotDto = new ParkingLotDto();

            parkingLotDto.Name     = "ParkingLot_A";
            parkingLotDto.Capacity = 30;
            parkingLotDto.Location = "Beijing";

            // When
            var           httpContent = JsonConvert.SerializeObject(parkingLotDto);
            StringContent content     = new StringContent(httpContent, Encoding.UTF8, MediaTypeNames.Application.Json);

            var response = await client.PostAsync("/parkingLots", content);

            var updateParkingLot = new ParkingLotUpdateDto();

            updateParkingLot.Capacity = 50;
            var           updateHttpContent      = JsonConvert.SerializeObject(updateParkingLot);
            StringContent updateContent          = new StringContent(updateHttpContent, Encoding.UTF8, MediaTypeNames.Application.Json);
            var           allParkingLotsResponse = await client.PatchAsync(response.Headers.Location, updateContent);

            // Then
            var searchResponse = await client.GetAsync(response.Headers.Location);

            var searchBody = await searchResponse.Content.ReadAsStringAsync();

            var searchParkingLotDto = JsonConvert.DeserializeObject <ParkingLotDto>(searchBody);

            Assert.Equal(updateParkingLot.Capacity, searchParkingLotDto.Capacity);
        }
Exemplo n.º 2
0
        public async Task <ParkingLotDto> UpdateParkingLotCapacity(int id, ParkingLotUpdateDto parkingLotUpdateDto)
        {
            var foundParkingLot =
                await parkingLotDbContext.ParkingLots.FirstOrDefaultAsync(parkingLotDto => parkingLotDto.Id == id);

            foundParkingLot.Capacity = parkingLotUpdateDto.Capacity;
            await this.parkingLotDbContext.SaveChangesAsync();

            return(new ParkingLotDto(foundParkingLot));
        }
        public async Task UpdateAsync(string id, ParkingLotUpdateDto parkingLotUpdateDto)
        {
            var parkingLotToUpdate = this.parkingLotContext.ParkingLots.FirstOrDefault(lot => lot.Id == id);

            if (parkingLotToUpdate != null)
            {
                var diff = parkingLotUpdateDto.Capacity - parkingLotToUpdate.Capacity;
                parkingLotToUpdate.Capacity           = parkingLotUpdateDto.Capacity;
                parkingLotToUpdate.AvailablePosition += diff;
            }

            await this.parkingLotContext.SaveChangesAsync();
        }
        public async Task Should_not_update_parking_lot_capacity_when_patch_update_with_negative_capacity()
        {
            var client            = GetClient();
            var parkingLot        = SeedParkingLot();
            var parkingLotContent = SerializeRequestBody(parkingLot);
            var createResponse    = await client.PostAsync(RootUri, parkingLotContent);

            createResponse.EnsureSuccessStatusCode();

            var lotUpdate = new ParkingLotUpdateDto()
            {
                Capacity = -1,
            };
            var updateContent  = SerializeRequestBody(lotUpdate);
            var updateResponse = await client.PatchAsync(createResponse.Headers.Location, updateContent);

            Assert.False(updateResponse.IsSuccessStatusCode);
            var getResponse = await client.GetAsync(createResponse.Headers.Location);

            getResponse.EnsureSuccessStatusCode();
            var newParkingLot = await DeserializeResponseBodyAsync <ParkingLotDto>(getResponse);

            Assert.Equal(parkingLot.Capacity, newParkingLot.Capacity);
        }
        public async Task <ActionResult> UpdateAsync(string id, ParkingLotUpdateDto parkingLotUpdateDto)
        {
            if (parkingLotUpdateDto.Capacity < 0)
            {
                return(BadRequest(new Dictionary <string, string>()
                {
                    { "error", "the capacity should not be minus" }
                }));
            }

            var parkingLotToUpdate = await this.parkingLotService.GetAsync(id);

            if (parkingLotToUpdate == null)
            {
                return(NotFound(new Dictionary <string, string>()
                {
                    { "error", "the parking lot is not found" }
                }));
            }

            await this.parkingLotService.UpdateAsync(id, parkingLotUpdateDto);

            return(NoContent());
        }
        public async Task <ActionResult> UpdateParkingLotCapacity(int id, ParkingLotUpdateDto parkingLotUpdateDto)
        {
            var updatedParkingLot = await parkingLotService.UpdateParkingLotCapacity(id, parkingLotUpdateDto);

            return(Ok(updatedParkingLot));
        }