One simple way to achieve your goal can be to create a settings table, where to specify the visibility of each field by group.
First you will need make a group(for brand) table like this:
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
}
then you will need a table for visibility settings:
public class TableVisibilitySettings
{
public int Id { get; set; }
public int GroupId { get; set; }
public virtual Group Group { get; set; }
public bool ContructionYear { get; set; }
public bool Power { get; set; }
public bool IsConvertible { get; set; }
}
Then you will need your table and the view model:
public class Table
{
public int Id { get; set; }
public int GroupId { get; set; }
public virtual Group Grup { get; set; }
public string Color { get; set; }
public int? ConstructionYear { get; set; }
public string Power { get; set; }
public bool? IsConvertible { get; set; }
public IEnumerable<TableVm> GetTableByGroupType(int groupId, ApplicationDbContext context)
{
var table = context.Tables.ToList();
var visibility = context.TableVisibilitySettings.FirstOrDefault(x => x.GroupId == groupId);
return table.Select(x => new TableVm
{
Id = x.Id,
Brand= x.Grup.Name,
Color = x.Color,
ConstructionYear = visibility.ContructionYear == true ? x.ConstructionYear : null,
Power = visibility.Power == true ? x.Power : null,
IsConvertible = visibility.IsConvertible == true ? x.IsConvertible : null
}).ToList();
}
}
Using the method GetTableByGroupType
you can retrieve the data base on the visibility settings for each group.
If you want you can use the Roles instead of Group.
Edit:
One way to apply pagination can be like this:
public IEnumerable<TableVm> GetTableByGroupWithPag(int groupId, ApplicationDbContext context,int pageNumber, int rowsPerPage)
{
var table = context.Tables.Skip((pageNumber-1)*rowsPerPage).Take(rowsPerPage).ToList();
var visibility = context.TableVisibilitySettings.FirstOrDefault(x => x.GroupId == groupId);
return table.Select(x => new TableVm
{
Id = x.Id,
Group = x.Grup.Name,
Color = x.Color,
ConstructionYear = visibility.ContructionYear == true ? x.ConstructionYear : null,
Power = visibility.Power == true ? x.Power : null,
IsConvertible = visibility.IsConvertible == true ? x.IsConvertible : null
}).ToList();
}
First you need to take the rows to display from your table, than you only need to apply the visibility settings.
Edit:
There are several ways to link a group to the user, depending of your application design and your skills.
The most simple way is to set a one to one
, or many to many
relations between ApplicationUser
and Group
, like this:
public class ApplicationUser
{
...
public int GroupId {get;set;}
public virtual Group Group
}
and in the Group class you need to add:
public virtual ICollection<ApplicationUser> Users {get;set;}
Another way is to create roles for each brand and to give each user one or more roles based on the brands which you want him to read/write.
Another way is to use Claims, and all you need to do is to add to each user a claim representing the groupId or the groupName or the brand.
Hope that this will help you chose a way to link the user to the group.