Example #1
0
    public async Task <ActionResult <AppRoleModel> > Update(
        [FromRoute] long id,
        [FromBody] AppRoleModel model
        )
    {
        try {
            var role = await roleMgr.FindByIdAsync(id.ToString());

            if (role == null)
            {
                return(NotFound());
            }
            mapper.Map(model, role);
            var result = await roleMgr.UpdateAsync(role);

            if (result.Succeeded)
            {
                mapper.Map(role, model);
                return(model);
            }
            return(BadRequest(result.GetErrorsString()));
        }
        catch (Exception ex) {
            logger.LogError(ex, $"Can not update role by id {id} with {model.ToJson()} .");
            return(this.InternalServerError(ex));
        }
    }
Example #2
0
        //Working
        public List <AppRoleModel> GrabUserAppRoles(string blazerId)
        {
            List <AppRoleModel> list = new List <AppRoleModel>();

            using (MyDbContext _db = new MyDbContext())
            {
                try
                {
                    List <int> roleIds = GrabUserAppRoleIds(blazerId);
                    foreach (var Id in roleIds)
                    {
                        var appRole      = _db.AppRoles.Single(x => x.Id == Id);
                        var appRoleModel = new AppRoleModel()
                        {
                            RoleId   = appRole.Id,
                            RoleName = appRole.Name
                        };
                        list.Add(appRoleModel);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                return(list);
            }
        }
Example #3
0
    public async Task <ActionResult <AppRoleModel> > Create(
        [FromBody] AppRoleModel model
        )
    {
        try {
            if (await roleMgr.RoleExistsAsync(model.Name))
            {
                return(BadRequest($"Role already {model.Name} exists!"));
            }
            var role   = mapper.Map <AppRole>(model);
            var result = await roleMgr.CreateAsync(role);

            if (result.Succeeded)
            {
                mapper.Map(role, model);
                return(model);
            }
            return(BadRequest(result.GetErrorsString()));
        }
        catch (Exception ex) {
            logger.LogError(ex, $"Can not create role with {model.ToJson()} .");
            return(this.InternalServerError(ex));
        }
    }
Example #4
0
        //Carefull this is for edit to get the existing rows
        public AppRoleModel BGrabAppRoles(int appId, string blazerId)
        {
            using (MyDbContext _db = new MyDbContext())
            {
                var appRoleModel = new AppRoleModel();
                try
                {
                    var userApp = _db.UserApps.FirstOrDefault(x => x.BlazerId == blazerId && x.AppId == appId);

                    if (userApp != null)
                    {
                        appRoleModel.RoleId   = userApp.RoleId;
                        appRoleModel.RoleName = GetAppRoleNameById(userApp.RoleId);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }

                return(appRoleModel);
            }
        }