Example #1
0
        public IActionResult CreateCity([FromBody] CityCreateModel city)
        {
            if (city == null)
            {
                BadRequest();
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            int newCityId = _citiesDataStore.Cities.Max(c => c.Id) + 1;

            var newCity = new CityData
            {
                Id          = newCityId,
                Name        = city.Name,
                Description = city.Description,
                NumberOfPointsOfInterest = city.NumberOfPointsOfInterest
            };

            _citiesDataStore.Cities.Add(newCity);

            return(CreatedAtRoute(
                       "GetCity", new { id = newCityId },
                       newCity));
        }
Example #2
0
        public IActionResult CreateCity([FromBody] CityCreateModel city)
        {
            if (city == null)
            {
                return(BadRequest());
            }

            //if (city.Description == city.Name)
            //{
            //	ModelState.AddModelError(
            //		"Description",
            //		"Description shouldn't be the same as Name.");
            //}

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            int newCityId = _citiesDataStore.Cities.Max(x => x.Id) + 1;

            var newCity = new CityData
            {
                Id          = newCityId,
                Name        = city.Name,
                Description = city.Description,
                NumberOfPointsOfInterest = city.NumberOfPointsOfInterest
            };

            _citiesDataStore.Cities.Add(newCity);

            return(CreatedAtRoute("GetCity", new { id = newCityId }, newCity));
        }
Example #3
0
        public IActionResult PutCity([FromBody] CityCreateModel cityCreateModel, int id)
        {
            var city = _citiesDataStore.Cities.Where(c => c.Id.Equals(id)).First();

            if (city.Equals(null))
            {
                NotFound();
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            city = new CityGetModel
            {
                Id          = id,
                Name        = cityCreateModel.Name,
                Description = cityCreateModel.Description,
                NumberOfPointsOfInterest = cityCreateModel.NumberOfPointsOfInterest
            };

            _citiesDataStore.Cities[city.Id - 1] = city;

            return(CreatedAtRoute("GetCity", new { id = city.Id }, city));
        }
Example #4
0
        public async Task <IActionResult> PostCity([FromBody] CityCreateModel model)
        {
            var command = Mapper.Map <CommandCreateCity>(model);
            await Mediator.Send(command);

            return(Ok());
        }
Example #5
0
        public IActionResult AddCity([FromBody] CityCreateModel cityCreateModel)
        {
            if (cityCreateModel == null)
            {
                return(BadRequest());
            }

            //собственная проверка
            //if (cityCreateModel.Name == cityCreateModel.Description)
            //	ModelState.AddModelError("Custom", "Description wrong");

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var citiesDataStore = CitiesDataStore.GetInstance();

            int  id   = citiesDataStore.Cities.Keys.Max() + 1;
            City city = cityCreateModel.ConvertToCity(id);

            citiesDataStore.Cities.Add(city.Id, city);

            return(Created($"/api/cities/{id}", new CityGetModel(city)));
        }
Example #6
0
        public IActionResult Create([FromForm] CityCreateModel entity)
        {
            if (ModelState.IsValid)
            {
                string currentUser = HttpContext?.User?.Identity?.Name;
                if (!String.IsNullOrEmpty(currentUser))
                {
                    try
                    {
                        AuditedEntityMapper <CityCreateModel> .FillCreateAuditedEntityFields(entity, currentUser, CountryId);

                        bool statusResult = _cityService.Add(entity);
                        if (statusResult)
                        {
                            return(RedirectToAction(nameof(Index)).WithSuccess(LOCALIZATION_SUCCESS_DEFAULT));
                        }
                        else
                        {
                            return(RedirectToAction(nameof(Index)).WithError(LOCALIZATION_ERROR_DEFAULT));
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, ex.Message);
                        return(RedirectToAction(nameof(Index)).WithError(ex.Message));
                    }
                }
                else
                {
                    _logger.LogError(LOCALIZATION_ERROR_USER_MUST_LOGIN);
                    return(NotFound().WithError(LOCALIZATION_ERROR_USER_MUST_LOGIN));
                }
            }
            return(View(entity).WithWarning(LOCALIZATION_WARNING_INVALID_MODELSTATE));
        }
        public IActionResult PutCity(int id, [FromBody] CityCreateModel cityModel)
        {
            if (cityModel == null)
            {
                BadRequest();
            }

            var citiesDataStore = CitiesDataStore.GetInstance();

            var city = citiesDataStore.Cities
                       .Where(x => x.Id == id)
                       .FirstOrDefault();

            if (city == null)
            {
                NotFound();
            }

            var index = citiesDataStore.Cities
                        .IndexOf(city);

            citiesDataStore.Cities[index].Name        = cityModel.Name;
            citiesDataStore.Cities[index].Description = cityModel.Description;
            citiesDataStore.Cities[index].NumberOfPointsOfInterest = cityModel.NumberOfPointsOfInterest;

            return(Ok(cityModel));
        }
Example #8
0
 public static City ToCityEntity(CityCreateModel model)
 {
     return(new City
     {
         Name = model.CityName,
         Id = model.CityNameId
     });
 }
Example #9
0
        public async Task <IActionResult> PatchCity([FromBody] CityCreateModel model, [FromRoute][Required] int id)
        {
            var command = Mapper.Map <CommandUpdateCity>(model);

            command.Id = id;
            await Mediator.Send(command);

            return(Ok());
        }
Example #10
0
        public CityGetModel AddCity(CityCreateModel newCity)
        {
            var          cityDatastore = CityDatastore.GetInstance();
            CityGetModel cityGetModel  = new CityGetModel();
            int          freeId        = GetFreeId();

            cityGetModel.Id                      = freeId;
            cityGetModel.Name                    = newCity.Name;
            cityGetModel.description             = newCity.description;
            cityGetModel.NumberOfPintsOfInterest = newCity.NumberOfPintsOfInterest;

            cityDatastore.Cities.Add(freeId, cityGetModel);
            return(cityGetModel);
        }
Example #11
0
        public IActionResult ReplaceCity(int id, [FromBody] CityCreateModel cityCreateModel)
        {
            var citiesDataStore = CitiesDataStore.GetInstance();

            if (citiesDataStore.Cities.ContainsKey(id))
            {
                citiesDataStore.Cities[id].Name        = cityCreateModel.Name;
                citiesDataStore.Cities[id].Description = cityCreateModel.Description;
                citiesDataStore.Cities[id].NumberOfPointsOfInterest = cityCreateModel.NumberOfPointsOfInterest;
                return(Ok(new CityGetModel(citiesDataStore.Cities[id])));
            }

            return(NotFound());
        }
Example #12
0
        public CityGetModel AddCity(CityCreateModel newCity)
        {
            int    freeId        = GetFreeId();
            string sqlExpression = $"INSERT INTO Cities (Id, Name, Description, NumberOfPointsOfInterest) " +
                                   $"VALUES ({freeId},'{newCity.Name}','{newCity.description}',{newCity.NumberOfPintsOfInterest})";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand(sqlExpression, connection);
                int        number  = command.ExecuteNonQuery();

                return(GetCityById(freeId));
            }
        }
Example #13
0
        public bool Add(CityCreateModel entity)
        {
            City city = new City
            {
                Name       = entity.Name,
                Postcode   = entity.Postcode,
                CreatedBy  = entity.CreatedBy,
                CreatedAt  = entity.CreatedAt,
                ModifiedAt = entity.ModifiedAt,
                ModifiedBy = entity.ModifiedBy,
                CountryId  = entity.CountryId
            };


            return(_repository.Add(city));
        }
        public async Task <IActionResult> CreateNewCity([Bind("CityName")] CityCreateModel cityCreateModel)
        {
            if (ModelState.IsValid)
            {
                var result            = ProgramMappings.ToCityEntity(cityCreateModel);
                var beforeCreatedCity = _repository.Query <City>().FirstOrDefault(x => x.Name == result.Name);
                if (beforeCreatedCity != null)
                {
                    _logger.LogWarning($"User {_user} tried to create existing city {result.Name}");
                    TempData["ErrorMessage"] = "Šis miestas jau yra sukurtas";
                    return(RedirectToAction(nameof(CreateNewCity)));
                }
                await _repository.InsertAsync <City>(result);

                _logger.LogInformation($"User {_user} created city {result.Name}");
                return(RedirectToAction(nameof(Index)));
            }
            _logger.LogError($"Error with ModelState validation. User {_user} ");
            return(View(cityCreateModel));
        }
Example #15
0
        public IActionResult UpdateCity(int id, [FromBody] CityCreateModel city)
        {
            if (city == null)
            {
                BadRequest();
            }

            var updatedCity = _citiesDataStore.Cities
                              .Where(x => x.Id == id)
                              .FirstOrDefault();

            if (updatedCity == null)
            {
                NotFound();
            }

            updatedCity.Name        = updatedCity.Name;
            updatedCity.Description = updatedCity.Description;
            updatedCity.NumberOfPointsOfInterest = updatedCity.NumberOfPointsOfInterest;

            return(NoContent());
        }
Example #16
0
        static void Main(string[] args)
        {
            //получим все города
            Console.WriteLine("Получим все города:");
            ApiOperations.GetAllCities();
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine();

            ////получим один город
            Console.WriteLine("Получим город по ид:");
            ApiOperations.GetCityById(1);
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine();

            //добавим новый город
            Console.WriteLine("Добавим город по строке:");
            string newCity = "{\"name\": \"Morshansk\", \"description\": \"Тамбовская область\", \"numberOfPintsOfInterest\": 150}";

            ApiOperations.AddCity(newCity);
            System.Threading.Thread.Sleep(1000);

            //добавим новый город по объекту
            Console.WriteLine("Добавим город по объекту:");
            CityCreateModel City = new CityCreateModel();

            City.Name                    = "Калуга";
            City.description             = "Описание";
            City.NumberOfPintsOfInterest = 100;
            ApiOperations.AddCity(City);
            System.Threading.Thread.Sleep(1000);

            //удалим город
            Console.WriteLine("Удалим город:");
            ApiOperations.DeleteCity(5);
            System.Threading.Thread.Sleep(1000);

            Console.WriteLine("Для завершения  програмы нажмите любую клавишу...");
            Console.ReadKey();
        }
        public IActionResult CreateCity([FromBody] CityCreateModel city)
        {
            if (city == null)
            {
                return(BadRequest());
            }

            var citiesDataStore = CitiesDataStore.GetInstance();
            int newCityId       = citiesDataStore.Cities.Max(x => x.Id) + 1;

            var newCity = new CityGetModel
            {
                Id          = newCityId,
                Name        = city.Name,
                Description = city.Description,
                NumberOfPointsOfInterest = city.NumberOfPointsOfInterest
            };

            citiesDataStore.Cities.Add(newCity);

            return(CreatedAtRoute("GetCity", new { id = newCityId }, newCity));
        }
Example #18
0
        public IActionResult AddCity([FromBody] CityCreateModel cityCreateModel)
        {
            if (cityCreateModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            int id = _citiesDataStore.Cities.Keys.Count == 0
                                ? 1
                                : _citiesDataStore.Cities.Keys.Max() + 1;

            City city = cityCreateModel.ConvertToCity(id);

            _citiesDataStore.Cities.Add(city.Id, city);

            return(Created($"/api/cities/{id}", new CityGetModel(city)));
        }
Example #19
0
        public IActionResult ReplaceCity(int id, [FromBody] CityCreateModel cityCreateModel)
        {
            if (id <= 0)
            {
                ModelState.AddModelError("id", "ID should be an integer value greater than 0.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_citiesDataStore.Cities.ContainsKey(id))
            {
                _citiesDataStore.Cities[id].Name        = cityCreateModel.Name;
                _citiesDataStore.Cities[id].Description = cityCreateModel.Description;
                _citiesDataStore.Cities[id].NumberOfPointsOfInterest = cityCreateModel.NumberOfPointsOfInterest;
                return(Ok(new CityGetModel(_citiesDataStore.Cities[id])));
            }

            return(NotFound());
        }
Example #20
0
        public IActionResult CreateCity([FromBody] CityCreateModel cityCreateModel)
        {
            if (cityCreateModel == null)
            {
                return(BadRequest());
            }

            if (_store.Cities.FirstOrDefault(c => c.Name == cityCreateModel.Name) != null)
            {
                return(Conflict());
            }

            int     newCityId = _store.Cities.Max(x => x.Id) + 1;
            CityDto cityDto   = cityCreateModel.ToDto(newCityId);

            _store.Cities.Add(cityDto);

            var cityGetModel = new CityGetModel(cityDto);

            return(CreatedAtRoute(
                       "GetCityById",
                       new { id = cityDto.Id },
                       cityGetModel));
        }
Example #21
0
 public IActionResult AddCity([FromBody] CityCreateModel city)
 {
     return(Ok(storageOperations.AddCity(city)));
 }
Example #22
0
        public void Post([FromBody] CityCreateModel model)
        {
            AuditedEntityMapper <CityCreateModel> .FillCreateAuditedEntityFields(model, HttpContext?.User?.Identity?.Name);

            Ok(_cityService.Add(model));
        }
Example #23
0
        //Добавим новый город по объекту
        public static async Task AddCity(CityCreateModel newCity)
        {
            string str = JsonConvert.SerializeObject(newCity);

            AddCity(str);
        }
        public IActionResult CreateNewCity()
        {
            var model = new CityCreateModel();

            return(View(model));
        }