コード例 #1
0
ファイル: MayorsController.cs プロジェクト: brenloria/CityAPI
        public IActionResult CreateMayor([FromBody] Mayors mayor)
        {
            var mayors   = MayorDataStore.Current.Mayors;
            var existing = mayors.FirstOrDefault(c => c.Name.ToLower().Equals(mayor.Name.ToLower()));

            if (existing != null)
            {
                ModelState.AddModelError("Name", "Mayor name must be unique.");
            }
            if (mayor.Age < 40)
            {
                ModelState.AddModelError("Age", "Mayor is underaged");
            }

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

            var lastID = mayors.Max(c => c.Id);

            var newMayor = new Mayors()
            {
                Id   = ++lastID,
                Name = mayor.Name,
                Age  = mayor.Age
            };

            mayors.Add(newMayor);
            return(CreatedAtRoute("GetMayor", new { mayorId = newMayor.Id }, newMayor));
        }
コード例 #2
0
ファイル: MayorsController.cs プロジェクト: brenloria/CityAPI
        public IActionResult UpdateCity(int id, [FromBody] Mayors mayorUpdate)
        {
            var mayor = MayorDataStore.Current.Mayors.FirstOrDefault(c => c.Id == id);

            if (mayor == null)
            {
                return(NotFound());
            }

            mayor.Name = mayorUpdate.Name;
            mayor.Age  = mayorUpdate.Age;

            return(NoContent());
        }