Skip to content

Commit

Permalink
Merge pull request #84 from Dilshodov01/FluentApi-ShamsheerDbContext
Browse files Browse the repository at this point in the history
ShamSherDbContext FluentApi updated   #52
  • Loading branch information
dotnetgoo authored Oct 28, 2023
2 parents d588753 + a9a87ea commit 84f5ac6
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 164 deletions.
124 changes: 93 additions & 31 deletions src/Shamsheer.Data/DbContexts/ShamsheerDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,101 @@
using Microsoft.EntityFrameworkCore;
using Shamsheer.Domain.Entities.Assets;
using Shamsheer.Domain.Entities.Authorizations.Channels;
using Shamsheer.Domain.Entities.Authorizations.Groups;
using Shamsheer.Domain.Entities.Chats;
using Shamsheer.Domain.Entities.Assets;
using Shamsheer.Domain.Entities.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Shamsheer.Domain.Entities.Authorizations.Groups;
using Shamsheer.Domain.Entities.Authorizations.Channels;

namespace Shamsheer.Data.DbContexts;

namespace Shamsheer.Data.DbContexts
public class ShamsheerDbContext : DbContext
{
public class ShamsheerDbContext : DbContext
public ShamsheerDbContext(DbContextOptions<ShamsheerDbContext> options)
: base(options)
{

}
public DbSet<User> Users { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<Message> Messages { get; set; }
public DbSet<Channel> Channels { get; set; }
public DbSet<UserGroup> UserGroups { get; set; }
public DbSet<UserAsset> UserAssets { get; set; }
public DbSet<GroupRole> GroupRoles { get; set; }
public DbSet<GroupAsset> GroupAssets { get; set; }
public DbSet<UserChannel> UserChannels { get; set; }
public DbSet<ChannelRole> ChannelRoles { get; set; }
public DbSet<ChannelAsset> ChannelAssets { get; set; }
public DbSet<MessageContent> MessageContents { get; set; }
public DbSet<GroupPermission> GroupPermissions { get; set; }
public DbSet<ChannelPermission> ChannelPermissions { get; set; }
public DbSet<GroupRolePermission> GroupRolesPermissions { get; set; }
public DbSet<ChannelRolePermission> ChannelRolesPermissions { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

modelBuilder.Entity<UserGroup>()
.HasOne(g => g.Group)
.WithMany(m => m.Members)
.HasForeignKey(g => g.MemberId);

modelBuilder.Entity<UserChannel>()
.HasOne(c => c.Channel)
.WithMany(s => s.Subscribers)
.HasForeignKey(c => c.ChannelId);

modelBuilder.Entity<UserAsset>()
.HasOne(u => u.User)
.WithMany(a => a.Assets)
.HasForeignKey(u => u.UserId);

modelBuilder.Entity<GroupAsset>()
.HasOne(g => g.Group)
.WithMany(a => a.Assets)
.HasForeignKey(g => g.GroupId);

modelBuilder.Entity<ChannelAsset>()
.HasOne(c => c.Channel)
.WithMany(a => a.Assets)
.HasForeignKey(c => c.ChannelId);

}
public void Configure(EntityTypeBuilder<Group> modelBuilder)
{
modelBuilder.ToTable("Groups");
modelBuilder.HasKey(g => g.Id);
modelBuilder.Property(g => g.OwnerId).IsRequired();
modelBuilder.Property(g => g.Title).HasMaxLength(64);
modelBuilder.Property(g => g.ChatType).IsRequired();
modelBuilder.Property(g => g.Username).HasMaxLength(64);
modelBuilder.Property(g => g.InviteLink).HasMaxLength(256);
modelBuilder.Property(g => g.Description).HasMaxLength(256);

}
public void Configure(EntityTypeBuilder<User> modelBuilder)
{
public ShamsheerDbContext(DbContextOptions<ShamsheerDbContext> options)
: base(options)
{

}
public DbSet<User> Users { get; set; }
public DbSet<Message> Messages { get; set; }
public DbSet<MessageContent> MessageContents { get; set; }
public DbSet<Channel> Channels { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<UserGroup> UserGroups { get; set; }
public DbSet<UserChannel> UserChannels { get; set; }
public DbSet<UserAsset> UserAssets { get; set; }
public DbSet<GroupAsset> GroupAssets { get; set; }
public DbSet<ChannelAsset> ChannelAssets { get; set; }
public DbSet<ChannelRole> ChannelRoles { get; set; }
public DbSet<ChannelPermission> ChannelPermissions { get; set; }
public DbSet<ChannelRolePermission> ChannelRolesPermissions { get; set; }
public DbSet<GroupRole> GroupRoles { get; set; }
public DbSet<GroupPermission> GroupPermissions { get; set; }
public DbSet<GroupRolePermission> GroupRolesPermissions { get; set; }
modelBuilder.ToTable("Users");
modelBuilder.HasKey(g => g.Id);
modelBuilder.Property(u => u.Email).HasMaxLength(50);
modelBuilder.Property(g => g.ChatType).IsRequired();
modelBuilder.Property(g => g.Phone).HasMaxLength(50);
modelBuilder.Property(g => g.Username).HasMaxLength(64);
modelBuilder.Property(g => g.Description).HasMaxLength(256);
modelBuilder.Property(g => g.FirstName).HasMaxLength(50).IsRequired();
modelBuilder.Property(g => g.LastName).HasMaxLength(50).HasMaxLength(64);
}

public void Configure(EntityTypeBuilder<Chat> modelBuilder)
{
modelBuilder.ToTable("Chats");
modelBuilder.HasKey(g => g.Id);
modelBuilder.Property(g => g.ChatType).IsRequired();
modelBuilder.Property(g => g.Username).HasMaxLength(64);
modelBuilder.Property(g => g.Description).HasMaxLength(256);

}


}

Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ public class ChannelPermissionsController : BaseController

public ChannelPermissionsController(IChannelPermissionService channelPermissionService)
{
_channelPermissionService = channelPermissionService;
this._channelPermissionService = channelPermissionService;
}

[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] ChannelPermissionType type)
=> Ok(await _channelPermissionService.CreateAsync(type));
=> Ok(await this._channelPermissionService.CreateAsync(type));

[HttpGet]
public async Task<IActionResult> GetAllAsync()
=> Ok(await _channelPermissionService.RetrieveAllAsync());
=> Ok(await this._channelPermissionService.RetrieveAllAsync());

[HttpGet("{id}")]
public async Task<IActionResult> GetAsync([FromRoute(Name = "id")] long id)
=> Ok(await _channelPermissionService.RetrieveByIdAsync(id));
=> Ok(await this._channelPermissionService.RetrieveByIdAsync(id));

[HttpPut("{id}")]
public async Task<IActionResult> PutAsync([FromRoute(Name = "id")] long id, [FromBody] ChannelPermissionType type)
=> Ok(await _channelPermissionService.ModifyAsync(id, type));
=> Ok(await this._channelPermissionService.ModifyAsync(id, type));

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteAsync([FromRoute(Name = "id")] long id)
=> Ok(await _channelPermissionService.RemoveAsync(id));
=> Ok(await this._channelPermissionService.RemoveAsync(id));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@
using Shamsheer.Domain.Enums.Chats;
using Shamsheer.Service.Interfaces.Authorizations.Channels;

namespace Shamsheer.Messenger.Api.Controllers.Authorizations.Channels
namespace Shamsheer.Messenger.Api.Controllers.Authorizations.Channels;

public class ChannelRolesController : BaseController
{
public class ChannelRolesController : BaseController
{
private readonly IChannelRoleService _channelRoleService;
private readonly IChannelRoleService _channelRoleService;

public ChannelRolesController(IChannelRoleService channelRoleService)
{
_channelRoleService = channelRoleService;
}
public ChannelRolesController(IChannelRoleService channelRoleService)
{
_channelRoleService = channelRoleService;
}

[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] ChatRole chatRole)
=> Ok(await _channelRoleService.CreateAsync(chatRole));
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] ChatRole chatRole)
=> Ok(await _channelRoleService.CreateAsync(chatRole));

[HttpGet()]
public async Task<IActionResult> GetAllAsync()
=> Ok(await _channelRoleService.RetrieveAllAsync());
[HttpGet]
public async Task<IActionResult> GetAllAsync()
=> Ok(await _channelRoleService.RetrieveAllAsync());

[HttpGet("{id}")]
public async Task<IActionResult> GetAsync([FromRoute(Name = "id")] long id)
=> Ok(await _channelRoleService.RetrieveByIdAsync(id));
[HttpGet("{id}")]
public async Task<IActionResult> GetAsync([FromRoute(Name = "id")] long id)
=> Ok(await _channelRoleService.RetrieveByIdAsync(id));

[HttpPut("{id}")]
public async Task<IActionResult> PutAsync([FromRoute(Name = "id")] long id, [FromBody] ChatRole chatRole)
=> Ok(await _channelRoleService.ModifyAsync(id, chatRole));
[HttpPut("{id}")]
public async Task<IActionResult> PutAsync([FromRoute(Name = "id")] long id, [FromBody] ChatRole chatRole)
=> Ok(await _channelRoleService.ModifyAsync(id, chatRole));

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteAsync([FromRoute(Name = "id")] long id)
=> Ok(await _channelRoleService.RemoveAsync(id));
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteAsync([FromRoute(Name = "id")] long id)
=> Ok(await _channelRoleService.RemoveAsync(id));
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ public class GroupPermissionsController : BaseController

public GroupPermissionsController(IGroupPermissionService groupPermissionService)
{
_groupPermissionService = groupPermissionService;
this._groupPermissionService = groupPermissionService;
}

[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] GroupPermissionType type)
=> Ok(await _groupPermissionService.CreateAsync(type));
=> Ok(await this._groupPermissionService.CreateAsync(type));

[HttpGet()]
[HttpGet]
public async Task<IActionResult> GetAllAsync()
=> Ok(await _groupPermissionService.RetrieveAllAsync());
=> Ok(await this._groupPermissionService.RetrieveAllAsync());

[HttpGet("{id}")]
public async Task<IActionResult> GetAsync([FromRoute(Name = "id")] long id)
=> Ok(await _groupPermissionService.RetrieveByIdAsync(id));
=> Ok(await this._groupPermissionService.RetrieveByIdAsync(id));

[HttpPut("{id}")]
public async Task<IActionResult> PutAsync([FromRoute(Name = "id")] long id, [FromBody] GroupPermissionType type)
=> Ok(await _groupPermissionService.ModifyAsync(id, type));
=> Ok(await this._groupPermissionService.ModifyAsync(id, type));

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteAsync([FromRoute(Name = "id")] long id)
=> Ok(await _groupPermissionService.RemoveAsync(id));
=> Ok(await this._groupPermissionService.RemoveAsync(id));
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ public class GroupRolesController : BaseController

public GroupRolesController(IGroupRoleService groupRoleService)
{
_groupRoleService = groupRoleService;
this._groupRoleService = groupRoleService;
}

[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] ChatRole chatRole)
=> Ok(await _groupRoleService.CreateAsync(chatRole));
=> Ok(await this._groupRoleService.CreateAsync(chatRole));

[HttpGet()]
[HttpGet]
public async Task<IActionResult> GetAllAsync()
=> Ok(await _groupRoleService.RetrieveAllAsync());
=> Ok(await this._groupRoleService.RetrieveAllAsync());

[HttpGet("{id}")]
public async Task<IActionResult> GetAsync([FromRoute(Name = "id")] long id)
=> Ok(await _groupRoleService.RetrieveByIdAsync(id));
=> Ok(await this._groupRoleService.RetrieveByIdAsync(id));

[HttpPut("{id}")]
public async Task<IActionResult> PutAsync([FromRoute(Name = "id")] long id, [FromBody] ChatRole chatRole)
=> Ok(await _groupRoleService.ModifyAsync(id, chatRole));
=> Ok(await this._groupRoleService.ModifyAsync(id, chatRole));

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteAsync([FromRoute(Name = "id")] long id)
=> Ok(await _groupRoleService.RemoveAsync(id));
=> Ok(await this._groupRoleService.RemoveAsync(id));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@
using Shamsheer.Service.DTOs.Channels;
using Shamsheer.Service.Interfaces.Channels;

namespace Shamsheer.Messenger.Api.Controllers.Channels
namespace Shamsheer.Messenger.Api.Controllers.Channels;

public class ChannelsController : BaseController
{
public class ChannelsController : BaseController
private readonly IChannelService _service;
public ChannelsController(IChannelService service)
{
private readonly IChannelService _service;
public ChannelsController(IChannelService service)
{
_service = service;
}
this._service = service;
}

[HttpPost]
public async Task<IActionResult> AddChannelAsync([FromBody] ChannelForCreationDto dto) =>
Ok(await _service.AddAsync(dto));
[HttpPost]
public async Task<IActionResult> AddChannelAsync([FromBody] ChannelForCreationDto dto) =>
Ok(await this._service.AddAsync(dto));

[HttpGet]
public async Task<IActionResult> GetAllChannelAsync() =>
Ok(await _service.RetrieveAllAsync());
[HttpGet]
public async Task<IActionResult> GetAllChannelAsync() =>
Ok(await this._service.RetrieveAllAsync());

[HttpGet("{id}")]
public async Task<IActionResult> GetByIdAsync([FromRoute(Name = "id")] long id) =>
Ok(await _service.RetrieveByIdAsync(id));
[HttpGet("{id}")]
public async Task<IActionResult> GetByIdAsync([FromRoute(Name = "id")] long id) =>
Ok(await this._service.RetrieveByIdAsync(id));

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteChannelAsync([FromRoute(Name = "id")] long id) =>
Ok(await _service.RemoveAsync(id));
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteChannelAsync([FromRoute(Name = "id")] long id) =>
Ok(await this._service.RemoveAsync(id));

[HttpPut("{id}")]
public async Task<IActionResult> UpdateChannelAsync([FromRoute(Name = "id")] long id, [FromBody] ChannelForUpdateDto dto) =>
Ok(await _service.ModifyAsync(id, dto));
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateChannelAsync([FromRoute(Name = "id")] long id, [FromBody] ChannelForUpdateDto dto) =>
Ok(await this._service.ModifyAsync(id, dto));
}
Loading

0 comments on commit 84f5ac6

Please sign in to comment.