예제 #1
0
        public IActionResult CreateGarage(GarageUpsertInput input)
        {
            _logger.LogInformation("Creating a garage", input);
            if (string.IsNullOrWhiteSpace(input.Name))
            {
                return(BadRequest(new Exception("Invalid input: name should be filled in")));
            }
            var garage = new Garage
            {
                // this is a bad idea, why?
                Id   = _database.Garages.Select(x => x.Id).Max() + 1,
                Cars = Array.Empty <Car>(),
                Name = input.Name,
            };

            _database.Garages.Add(garage);
            return(Created($"/garages/{garage.Id}", new GarageWebOutput(garage.Id, garage.Name)));
        }
예제 #2
0
        public IActionResult UpdateGarage(int id, GarageUpsertInput input)
        {
            _logger.LogInformation("Updating a garage", input);
            if (string.IsNullOrWhiteSpace(input.Name))
            {
                return(BadRequest(new Exception("Invalid input: name should be filled in")));
            }
            if (id == 0)
            {
                return(BadRequest(new Exception("Invalid input: id should not be equal to zero")));
            }
            // The next line is bad; why?
            var garage = _database.Garages.FirstOrDefault(x => x.Id == id);

            if (garage == null)
            {
                return(NotFound());
            }
            garage.Name = input.Name;
            return(Accepted(new GarageWebOutput(garage.Id, garage.Name)));
        }