コード例 #1
0
        public async Task <IActionResult> PostRoad([FromBody] RequestRoad road)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var newRoad = new GoogleRoad()
            {
                Paths     = road.Paths,
                Direction = road.Direction,
                Distance  = road.Distance,
                Color     = road.Color,
                Name      = road.Name,
                MapId     = road.MapId
            };

            try
            {
                _context.GoogleRoads.Add(newRoad);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetRoad", new { id = newRoad.Id }, newRoad));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
コード例 #2
0
        public async Task <IActionResult> PutRoads([FromRoute] int id, [FromBody] List <RequestRoad> data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id > 0)
            {
                var road = data[0];
                if (id != road.Id)
                {
                    return(BadRequest());
                }
                var r = new GoogleRoad()
                {
                    Id        = road.Id,
                    Name      = road.Name,
                    Direction = road.Direction,
                    Distance  = road.Distance,
                    Color     = road.Color,
                    Paths     = road.Paths,
                    MapId     = road.MapId
                };

                _context.Entry(r).State = EntityState.Modified;

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoadExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else if (id == -1)
            {
                for (var i = 0; i < data.Count; i++)
                {
                    var road = new GoogleRoad()
                    {
                        Id        = data[i].Id,
                        Name      = data[i].Name,
                        Direction = data[i].Direction,
                        Distance  = data[i].Distance,
                        Color     = data[i].Color,
                        Paths     = data[i].Paths,
                        MapId     = data[i].MapId
                    };

                    _context.Entry(road).State = EntityState.Modified;

                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!RoadExists(data[i].Id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            return(Ok("OK"));
        }