Example #1
0
        public async Task <IActionResult> PutMapType([FromRoute] int id, [FromBody] MapType mapType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            return(NoContent());
        }
Example #2
0
        public async Task <IActionResult> UpdateUser([FromRoute] long id, [FromBody] UserModel.UserForCreate user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

            var originUser = _context.User.SingleOrDefault(u => u.Id == id);

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

            originUser.UserName           = user.Username;
            originUser.NormalizedEmail    = user.EmailAddress.ToUpper();
            originUser.Name               = user.Name;
            originUser.IsActive           = user.IsActive;
            originUser.Surname            = user.Surname;
            originUser.NormalizedUserName = user.Username.ToUpper();
            originUser.Email              = user.EmailAddress;
            originUser.EmailConfirmed     = user.SendActivationEmail;

            if (!string.IsNullOrEmpty(user.Password))
            {
                var passwd = _userManager.PasswordHasher.HashPassword(originUser, user.Password);
                originUser.PasswordHash = passwd;
            }

            try
            {
                await _context.SaveChangesAsync();

                var currentUserRoles = _context.UserRoles.Where(r => r.UserId == user.Id);

                _context.UserRoles.RemoveRange(currentUserRoles);

                foreach (var roleName in user.AssignedRoleNames)
                {
                    var role = _context.Role.SingleOrDefault(r => r.RoleName == roleName);
                    if (role != null)
                    {
                        await _context.UserRoles.AddAsync(new UserRole(originUser.Id, role.Id));
                    }
                }
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetUser", new { id = originUser.Id }, originUser));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
Example #3
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"));
        }
Example #4
0
        public async Task <IActionResult> PutRole([FromRoute] int id, [FromBody] RoleModel.RoleForUpdate role)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != role.Id)
            {
                return(BadRequest());
            }
            var originRole = _context.Role.SingleOrDefault(a => a.Id == id);

            if (originRole != null)
            {
                originRole.RoleName             = role.RoleName;
                originRole.Descriptions         = role.Descriptions;
                originRole.LastModifierUserId   = role.LastModifierUserId;
                originRole.LastModificationTime = DateTime.Now;
            }
            //_context.Entry(role).State = EntityState.Modified;

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

            return(CreatedAtAction("GetRole", new { id = role.Id }, role));
        }
        public async Task <IActionResult> AddOrUpdatePermission([FromRoute] int id, [FromBody] PermissionModel.PermissionWithCategor permission)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (PermissionRoleExists(permission.Id, id))
            {
                var per = await _context.PermissionRoles.SingleOrDefaultAsync(m => m.PermissionId == permission.Id && m.RoleId == id);

                if (per == null)
                {
                    return(NotFound());
                }
                _context.PermissionRoles.Remove(per);
            }
            else
            {
                _context.PermissionRoles.Add(new PermissionRole()
                {
                    PermissionId = permission.Id, RoleId = id
                });
            }

            try
            {
                await _context.SaveChangesAsync();

                return(Ok(true));
            }
            catch (Exception ex)
            {
                return(Ok(false));
            }
        }
Example #6
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"));
        }