/// <summary>
 ///
 /// </summary>
 public AdminController()
 {
     _client                = MH.GetClient();
     role_db                = _client.GetDatabase("RoleDB");
     roles_collection       = role_db.GetCollection <BsonDocument>("Roles");
     log_db                 = _client.GetDatabase("LogDB");
     activitylog_collection = log_db.GetCollection <ActivityLoggerModel>("ActivityLog");
     rolesCollection        = role_db.GetCollection <Roles>("Roles");
 }
        public ActionResult UpdateRole([FromBody] ExampleModel_Roles data, string username, string rolename)
        {
            try
            {
                if (data != null && username != null && rolename != null)
                {
                    var getRole = MH.GetSingleObject(roles_collection, "RoleName", rolename, null, null).Result;
                    if (getRole != null)
                    {
                        if (data.LevelOfAccess != null)
                        {
                            var updateDefinition = Builders <BsonDocument> .Update.Set("LevelOfAccess", data.LevelOfAccess);

                            var update = MH.UpdateSingleObject(roles_collection, "RoleName", rolename, null, null, updateDefinition);
                        }
                        if (data.IsActive != null)
                        {
                            var updateDefinition = Builders <BsonDocument> .Update.Set("IsActive", data.IsActive);

                            var update = MH.UpdateSingleObject(roles_collection, "RoleName", rolename, null, null, updateDefinition);
                        }
                        AL.CreateLog(username, "UpdateRole", BsonSerializer.Deserialize <Roles>(getRole), data, activitylog_collection);
                        return(Ok(new ResponseData
                        {
                            Code = "200",
                            Message = "Updated"
                        }));
                    }
                    else
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "404",
                            Message = "Role not found"
                        }));
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "401",
                        Message = "Bad request"
                    }));
                }
            }
            catch (Exception ex)
            {
                SL.CreateLog("AdminController", "UpdateRole", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
        public ActionResult MakeRoleActive(string username, string rolename)
        {
            try
            {
                if (username != null && rolename != null)
                {
                    var getRole = MH.GetSingleObject(roles_collection, "RoleName", rolename, null, null).Result;
                    if (getRole != null)
                    {
                        var updateDefinition = Builders <BsonDocument> .Update.Set("IsActive", true);

                        update = MH.UpdateSingleObject(roles_collection, "RoleName", rolename, null, null, updateDefinition);
                        var data = BsonSerializer.Deserialize <Roles>(getRole);
                        data.IsActive = true;
                        AL.CreateLog(username, "MakeRoleActive", BsonSerializer.Deserialize <Roles>(getRole), data, activitylog_collection);
                        return(Ok(new ResponseData
                        {
                            Code = "200",
                            Message = "Role made active"
                        }));
                    }
                    else
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "404",
                            Message = "Role not found"
                        }));
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "401",
                        Message = "Bad Request"
                    }));
                }
            }
            catch (Exception ex)
            {
                SL.CreateLog("AdminController", "MakeRoleActive", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
 public ActionResult GetRole(string rolename)
 {
     try
     {
         if (rolename != null)
         {
             var getRole = MH.GetSingleObject(roles_collection, "RoleName", rolename, null, null).Result;
             if (getRole != null)
             {
                 return(Ok(new ResponseData
                 {
                     Code = "200",
                     Message = "Success",
                     Data = BsonSerializer.Deserialize <Roles>(getRole)
                 }));
             }
             else
             {
                 return(BadRequest(new ResponseData
                 {
                     Code = "404",
                     Message = "Role not found"
                 }));
             }
         }
         else
         {
             return(BadRequest(new ResponseData
             {
                 Code = "401",
                 Message = "Bad Request"
             }));
         }
     }
     catch (Exception ex)
     {
         SL.CreateLog("AdminController", "GetRole", ex.Message);
         return(BadRequest(new ResponseData
         {
             Code = "400",
             Message = "Failed",
             Data = ex.Message
         }));
     }
 }
 public ActionResult GetAllRoles()
 {
     try
     {
         var getRoles = MH.GetListOfObjects(roles_collection, null, null, null, null).Result;
         if (getRoles != null)
         {
             List <Roles> roles = new List <Roles>();
             foreach (var role in getRoles)
             {
                 roles.Add(BsonSerializer.Deserialize <Roles>(role));
             }
             return(Ok(new ResponseData
             {
                 Code = "200",
                 Message = "Success",
                 Data = roles
             }));
         }
         else
         {
             return(BadRequest(new ResponseData
             {
                 Code = "404",
                 Message = "No roles found"
             }));
         }
     }
     catch (Exception ex)
     {
         SL.CreateLog("AdminController", "GetAllRoles", ex.Message);
         return(BadRequest(new ResponseData
         {
             Code = "400",
             Message = "Failed",
             Data = ex.Message
         }));
     }
 }
        public ActionResult InsertRole([FromBody] Roles data, string username)
        {
            try
            {
                if (data != null && username != null)
                {
                    var getRole = MH.GetSingleObject(roles_collection, "RoleName", data.RoleName, null, null).Result;
                    if (getRole == null)
                    {
                        data.IsActive = true;
                        var insert = MH.InsertNewRole(data, rolesCollection);
                        if (insert == true)
                        {
                            AL.CreateLog(username, "InsertRole", null, data, activitylog_collection);
                            return(Ok(new ResponseData
                            {
                                Code = "200",
                                Message = "Inserted",
                                Data = data
                            }));
                        }
                        else if (insert == false)
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "403",
                                Message = "Role with same id is already added"
                            }));
                        }
                        else
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "400",
                                Message = "Failed",
                                Data = insert
                            }));
                        }
                    }
                    else
                    {
                        var roleDetails = BsonSerializer.Deserialize <Roles>(getRole);
                        if (roleDetails.IsActive == true)
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "401",
                                Message = "Role with the same name is found"
                            }));
                        }
                        else
                        {
                            var updateDefinition = Builders <BsonDocument> .Update.Set("IsActive", true);

                            update = MH.UpdateSingleObject(roles_collection, "RoleName", data.RoleName, null, null, updateDefinition);
                            var detail = roleDetails;
                            detail.IsActive = true;
                            AL.CreateLog(username, "InsertRole", roleDetails, detail, activitylog_collection);
                            return(BadRequest(new ResponseData
                            {
                                Code = "402",
                                Message = "Role with the same name is found and is made active"
                            }));
                        }
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "405",
                        Message = "Bad request"
                    }));
                }
            }
            catch (Exception ex)
            {
                SL.CreateLog("AdminController", "InsertRole", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }