Ejemplo n.º 1
0
        public IHttpActionResult InsertRoleData(EntityRoles model)
        {
            var response = new DataResponse <EntityRoles>();

            if (ModelState.IsValid)
            {
                model.CreatedBy  = CurrentUserId;
                model.BusinessId = CurrentBusinessId;
                if (model.Id > 0)
                {
                    response = new RepositoryRoles().Update(model);
                }
                else
                {
                    response = new RepositoryRoles().Insert(model);
                }
                return(Ok <DataResponse>(response));
            }
            else
            {
                var errorList = ModelState.Where(a => a.Value.Errors.Any()).Select(s => new
                {
                    Key     = s.Key.Split('.').Last(),
                    Message = s.Value.Errors[0].ErrorMessage
                });
                return(Ok <dynamic>(new { Status = HttpStatusCode.BadRequest, Model = errorList }));
            }
        }
        public DataResponse <EntityRoles> Insert(EntityRoles entity)
        {
            var response = new DataResponse <EntityRoles>();

            try
            {
                base.DBInit();

                var model = new Database.Role
                {
                    Name        = entity.Name,
                    Description = entity.Description,
                    BusinessId  = entity.BusinessId,
                    Status      = 1,
                    IsActive    = true,
                    CreatedOn   = entity.CreatedOn,
                    CreatedBy   = entity.CreatedBy
                };

                if (base.DBSave(model) > 0)
                {
                    if (entity.RolePrivilegeIds != null && entity.RolePrivilegeIds.Count() > 0)
                    {
                        foreach (var item in entity.RolePrivilegeIds)
                        {
                            DBEntity.RolePrivileges.Add(new RolePrivilege {
                                RoleId = model.Id, PrivilegeId = item, CreatedBy = entity.CreatedBy.Value, CreatedOn = entity.CreatedOn
                            });
                            DBEntity.SaveChanges();
                        }
                    }
                    entity.Id = model.Id;
                    response.CreateResponse(entity, DataResponseStatus.OK);
                }
                else
                {
                    response.CreateResponse(DataResponseStatus.InternalServerError);
                }
            }
            catch (Exception ex)
            {
                ex.Log();
            }
            finally
            {
                base.DBClose();
            }
            return(response);
        }
        public DataResponse <EntityRoles> Update(EntityRoles entity)
        {
            var response = new DataResponse <EntityRoles>();

            try
            {
                base.DBInit();

                var model = DBEntity.Roles.FirstOrDefault(a => a.Id == entity.Id);
                model.Name        = entity.Name;
                model.Description = entity.Description;
                model.IsActive    = entity.IsActive;

                if (base.DBSaveUpdate(model) > 0)
                {
                    IEnumerable <RolePrivilege> RolesPrivilege = DBEntity.RolePrivileges.Where(a => a.RoleId == model.Id).ToList();
                    if (RolesPrivilege.Count() > 0)
                    {
                        DBEntity.RolePrivileges.RemoveRange(RolesPrivilege);
                        DBEntity.SaveChanges();
                    }
                    if (entity.RolePrivilegeIds != null && entity.RolePrivilegeIds.Count() > 0)
                    {
                        foreach (var item in entity.RolePrivilegeIds)
                        {
                            DBEntity.RolePrivileges.Add(new RolePrivilege {
                                RoleId = model.Id, PrivilegeId = item, CreatedBy = entity.CreatedBy.Value, CreatedOn = entity.CreatedOn
                            });
                            DBEntity.SaveChanges();
                        }
                    }
                    return(GetRolesById(model.Id));
                }
                else
                {
                    response.CreateResponse(DataResponseStatus.InternalServerError);
                }
            }
            catch (Exception ex)
            {
                ex.Log();
            }
            finally
            {
                base.DBClose();
            }
            return(response);
        }
        public IHttpActionResult CreateRoles(RolesModel model)
        {
            var response = new DataResponse <EntityRoles>();

            var entityRoles = new EntityRoles();

            entityRoles.Name          = model.Name;
            entityRoles.Description   = model.Description;
            entityRoles.BusinessId    = CurrentBusinessId;
            entityRoles.CreatedBy     = CurrentUserId;
            entityRoles.CreatedOn     = DateTime.UtcNow;
            entityRoles.RolePrivilege = model.RolePrivilege;
            response = repository.Insert(entityRoles);

            return(Ok <DataResponse>(response));
        }
        public IHttpActionResult UpdateRoles(RolesModel model)
        {
            var response = new DataResponse <EntityRoles>();

            if (ModelState.IsValid)
            {
                var entityRoles = new EntityRoles();
                entityRoles.Id            = model.Id;
                entityRoles.Name          = model.Name;
                entityRoles.Description   = model.Description;
                entityRoles.IsActive      = model.IsActive;
                entityRoles.RolePrivilege = model.RolePrivilege;
                entityRoles.CreatedBy     = CurrentUserId;

                response = repository.Update(entityRoles);
            }
            return(Ok <DataResponse>(response));
        }