Exemplo n.º 1
0
        public async Task <IActionResult> PutMap([FromRoute] int id, [FromBody] MapModel.MapUpdateEdit map)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != map.Id)
            {
                return(BadRequest());
            }

            var originMap = _context.Maps.SingleOrDefault(x => x.Id == map.Id);

            if (originMap != null)
            {
                originMap.Name         = map.Name;
                originMap.Descriptions = map.Descriptions;
                originMap.MapTypeId    = map.Type;
            }

            try
            {
                await _context.SaveChangesAsync();

                var mapRoles = _context.MapRoles.Where(x => x.MapId == id);

                _context.MapRoles.RemoveRange(mapRoles);

                await _context.SaveChangesAsync();

                foreach (var roleName in map.RolesAssigned)
                {
                    var role = _context.Role.SingleOrDefault(x => x.RoleName == roleName);
                    if (role != null)
                    {
                        _context.MapRoles.Add(new MapRole {
                            MapId = id, RoleId = role.Id
                        });

                        await _context.SaveChangesAsync();
                    }
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MapExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok("OK"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PostMap([FromBody] MapModel.MapUpdateEdit map)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var newMap = new Map {
                Name = map.Name, MapTypeId = map.Type, Descriptions = map.Descriptions
            };

            try
            {
                _context.Maps.Add(newMap);

                await _context.SaveChangesAsync();

                var mapRoles = _context.MapRoles.Where(x => x.MapId == newMap.Id);

                _context.MapRoles.RemoveRange(mapRoles);

                await _context.SaveChangesAsync();

                foreach (var roleName in map.RolesAssigned)
                {
                    var role = _context.Role.SingleOrDefault(x => x.RoleName == roleName);
                    if (role != null)
                    {
                        _context.MapRoles.Add(new MapRole {
                            MapId = newMap.Id, RoleId = role.Id
                        });

                        await _context.SaveChangesAsync();
                    }
                }

                return(CreatedAtAction("GetMaps", new { id = newMap.Id }, newMap));
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }