refactoring for integration
This commit is contained in:
parent
80144bf9fc
commit
070f41ac7e
48 changed files with 100 additions and 46 deletions
164
src/Yavsc.Api/Controllers/Relationship/BlackListApiController.cs
Normal file
164
src/Yavsc.Api/Controllers/Relationship/BlackListApiController.cs
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Access;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/blacklist"), Authorize]
|
||||
public class BlackListApiController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public BlackListApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/BlackListApi
|
||||
[HttpGet]
|
||||
public IEnumerable<BlackListed> GetBlackListed()
|
||||
{
|
||||
return _context.BlackListed;
|
||||
}
|
||||
|
||||
// GET: api/BlackListApi/5
|
||||
[HttpGet("{id}", Name = "GetBlackListed")]
|
||||
public IActionResult GetBlackListed([FromRoute] long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
BlackListed blackListed = _context.BlackListed.Single(m => m.Id == id);
|
||||
if (blackListed == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
if (!CheckPermission(blackListed))
|
||||
return BadRequest();
|
||||
|
||||
return Ok(blackListed);
|
||||
}
|
||||
|
||||
private bool CheckPermission(BlackListed blackListed)
|
||||
{
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (uid != blackListed.OwnerId)
|
||||
if (!User.IsInRole(YavscConstants.AdminGroupName))
|
||||
if (!User.IsInRole(YavscConstants.FrontOfficeGroupName))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
// PUT: api/BlackListApi/5
|
||||
[HttpPut("{id}")]
|
||||
public IActionResult PutBlackListed(long id, [FromBody] BlackListed blackListed)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (id != blackListed.Id)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
if (!CheckPermission(blackListed))
|
||||
return BadRequest();
|
||||
_context.Entry(blackListed).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!BlackListedExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
|
||||
// POST: api/BlackListApi
|
||||
[HttpPost]
|
||||
public IActionResult PostBlackListed([FromBody] BlackListed blackListed)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (!CheckPermission(blackListed))
|
||||
return BadRequest();
|
||||
|
||||
_context.BlackListed.Add(blackListed);
|
||||
try
|
||||
{
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (BlackListedExists(blackListed.Id))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetBlackListed", new { id = blackListed.Id }, blackListed);
|
||||
}
|
||||
|
||||
// DELETE: api/BlackListApi/5
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult DeleteBlackListed(long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
BlackListed blackListed = _context.BlackListed.Single(m => m.Id == id);
|
||||
if (blackListed == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (!CheckPermission(blackListed))
|
||||
return BadRequest();
|
||||
|
||||
_context.BlackListed.Remove(blackListed);
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
|
||||
return Ok(blackListed);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private bool BlackListedExists(long id)
|
||||
{
|
||||
return _context.BlackListed.Count(e => e.Id == id) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
165
src/Yavsc.Api/Controllers/Relationship/BlogAclApiController.cs
Normal file
165
src/Yavsc.Api/Controllers/Relationship/BlogAclApiController.cs
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Access;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/blogacl")]
|
||||
public class BlogAclApiController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public BlogAclApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/BlogAclApi
|
||||
[HttpGet]
|
||||
public IEnumerable<CircleAuthorizationToBlogPost> GetBlogACL()
|
||||
{
|
||||
return _context.CircleAuthorizationToBlogPost;
|
||||
}
|
||||
|
||||
// GET: api/BlogAclApi/5
|
||||
[HttpGet("{id}", Name = "GetCircleAuthorizationToBlogPost")]
|
||||
public async Task<IActionResult> GetCircleAuthorizationToBlogPost([FromRoute] long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
CircleAuthorizationToBlogPost circleAuthorizationToBlogPost = await _context.CircleAuthorizationToBlogPost.SingleAsync(
|
||||
m => m.CircleId == id && m.Allowed.OwnerId == uid );
|
||||
|
||||
if (circleAuthorizationToBlogPost == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(circleAuthorizationToBlogPost);
|
||||
}
|
||||
|
||||
// PUT: api/BlogAclApi/5
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutCircleAuthorizationToBlogPost([FromRoute] long id, [FromBody] CircleAuthorizationToBlogPost circleAuthorizationToBlogPost)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (id != circleAuthorizationToBlogPost.CircleId)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
if (!CheckOwner(circleAuthorizationToBlogPost.CircleId))
|
||||
{
|
||||
return new ChallengeResult();
|
||||
}
|
||||
_context.Entry(circleAuthorizationToBlogPost).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!CircleAuthorizationToBlogPostExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
private bool CheckOwner (long circleId)
|
||||
{
|
||||
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var circle = _context.Circle.First(c=>c.Id==circleId);
|
||||
_context.Entry(circle).State = EntityState.Detached;
|
||||
return (circle.OwnerId == uid);
|
||||
}
|
||||
// POST: api/BlogAclApi
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostCircleAuthorizationToBlogPost([FromBody] CircleAuthorizationToBlogPost circleAuthorizationToBlogPost)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
if (!CheckOwner(circleAuthorizationToBlogPost.CircleId))
|
||||
{
|
||||
return new ChallengeResult();
|
||||
}
|
||||
_context.CircleAuthorizationToBlogPost.Add(circleAuthorizationToBlogPost);
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (CircleAuthorizationToBlogPostExists(circleAuthorizationToBlogPost.CircleId))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetCircleAuthorizationToBlogPost", new { id = circleAuthorizationToBlogPost.CircleId }, circleAuthorizationToBlogPost);
|
||||
}
|
||||
|
||||
// DELETE: api/BlogAclApi/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteCircleAuthorizationToBlogPost([FromRoute] long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
|
||||
CircleAuthorizationToBlogPost circleAuthorizationToBlogPost = await _context.CircleAuthorizationToBlogPost.Include(
|
||||
a=>a.Allowed
|
||||
).SingleAsync(m => m.CircleId == id
|
||||
&& m.Allowed.OwnerId == uid);
|
||||
if (circleAuthorizationToBlogPost == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
_context.CircleAuthorizationToBlogPost.Remove(circleAuthorizationToBlogPost);
|
||||
await _context.SaveChangesAsync(User.GetUserId());
|
||||
|
||||
return Ok(circleAuthorizationToBlogPost);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private bool CircleAuthorizationToBlogPostExists(long id)
|
||||
{
|
||||
return _context.CircleAuthorizationToBlogPost.Count(e => e.CircleId == id) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
114
src/Yavsc.Api/Controllers/Relationship/ChatApiController.cs
Normal file
114
src/Yavsc.Api/Controllers/Relationship/ChatApiController.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.ViewModels.Chat;
|
||||
using Yavsc.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
|
||||
[Route("api/chat")]
|
||||
public class ChatApiController : Controller
|
||||
{
|
||||
readonly ApplicationDbContext dbContext;
|
||||
readonly UserManager<ApplicationUser> userManager;
|
||||
private readonly IConnexionManager _cxManager;
|
||||
public ChatApiController(ApplicationDbContext dbContext,
|
||||
UserManager<ApplicationUser> userManager,
|
||||
IConnexionManager cxManager)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.userManager = userManager;
|
||||
_cxManager = cxManager;
|
||||
}
|
||||
|
||||
[HttpGet("users")]
|
||||
public IEnumerable<ChatUserInfo> GetUserList()
|
||||
{
|
||||
List<ChatUserInfo> result = new List<ChatUserInfo>();
|
||||
var cxsQuery = dbContext.ChatConnection?.Include(c => c.Owner)
|
||||
.Where(cx => cx.Connected).GroupBy(c => c.ApplicationUserId);
|
||||
|
||||
if (cxsQuery != null)
|
||||
foreach (var g in cxsQuery)
|
||||
{
|
||||
|
||||
var uid = g.Key;
|
||||
var cxs = g.ToList();
|
||||
if (cxs != null)
|
||||
if (cxs.Count > 0)
|
||||
{
|
||||
var user = cxs.First().Owner;
|
||||
if (user != null)
|
||||
{
|
||||
result.Add(new ChatUserInfo
|
||||
{
|
||||
UserName = user.UserName,
|
||||
UserId = user.Id,
|
||||
Avatar = user.Avatar,
|
||||
Connections = cxs,
|
||||
Roles = (userManager.GetRolesAsync(user)).Result.ToArray()
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(new ChatUserInfo { Connections = cxs });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// GET: api/chat/userName
|
||||
[HttpGet("user/{userName}", Name = "uinfo")]
|
||||
public IActionResult GetUserInfo([FromRoute] string userName)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
// Miguel mech profiler
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var user = dbContext.ApplicationUser.Include(u => u.Connections).FirstOrDefault(u => u.UserName == userName);
|
||||
|
||||
if (user == null) return NotFound();
|
||||
|
||||
return Ok(new ChatUserInfo
|
||||
{
|
||||
UserName = user.UserName,
|
||||
UserId = user.Id,
|
||||
Avatar = user.Avatar,
|
||||
Connections = user.Connections,
|
||||
Roles = (userManager.GetRolesAsync(user)).Result.ToArray()
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get firsts 10 biggest channels having
|
||||
/// a name starting with given prefix.
|
||||
/// </summary>
|
||||
/// <param name="chanNamePrefix">chan Name Prefix</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("chanlist/{chanNamePrefix}")]
|
||||
public IActionResult GetChanList([FromRoute] string chanNamePrefix)
|
||||
{
|
||||
var list = _cxManager.ListChannels(chanNamePrefix);
|
||||
return Ok(list);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get firsts 10 biggest channels
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("chanlist")]
|
||||
public IActionResult GetChanList()
|
||||
{
|
||||
return Ok(_cxManager.ListChannels(null));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,183 @@
|
|||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Chat;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/ChatRoomAccessApi")]
|
||||
public class ChatRoomAccessApiController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public ChatRoomAccessApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/ChatRoomAccessApi
|
||||
[HttpGet, Authorize("AdministratorOnly")]
|
||||
public IEnumerable<ChatRoomAccess> GetChatRoomAccess()
|
||||
{
|
||||
return _context.ChatRoomAccess;
|
||||
}
|
||||
|
||||
// GET: api/ChatRoomAccessApi/5
|
||||
[HttpGet("{id}", Name = "GetChatRoomAccess"), Authorize("AdministratorOnly")]
|
||||
public async Task<IActionResult> GetChatRoomAccess([FromRoute] string id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
ChatRoomAccess chatRoomAccess = await _context.ChatRoomAccess.SingleAsync(m => m.ChannelName == id);
|
||||
|
||||
|
||||
|
||||
if (chatRoomAccess == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
if (uid != chatRoomAccess.UserId && uid != chatRoomAccess.Room.OwnerId
|
||||
&& ! User.IsInMsRole(YavscConstants.AdminGroupName))
|
||||
|
||||
{
|
||||
ModelState.AddModelError("UserId","get refused");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
return Ok(chatRoomAccess);
|
||||
}
|
||||
|
||||
// PUT: api/ChatRoomAccessApi/5
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutChatRoomAccess([FromRoute] string id, [FromBody] ChatRoomAccess chatRoomAccess)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
|
||||
if (id != chatRoomAccess.ChannelName)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
var room = _context.ChatRoom.First(channel => channel.Name == chatRoomAccess.ChannelName );
|
||||
|
||||
if (uid != room.OwnerId && ! User.IsInMsRole(YavscConstants.AdminGroupName))
|
||||
{
|
||||
ModelState.AddModelError("ChannelName", "access put refused");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Entry(chatRoomAccess).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!ChatRoomAccessExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
|
||||
// POST: api/ChatRoomAccessApi
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostChatRoomAccess([FromBody] ChatRoomAccess chatRoomAccess)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var room = _context.ChatRoom.First(channel => channel.Name == chatRoomAccess.ChannelName );
|
||||
if (room == null || (uid != room.OwnerId && ! User.IsInMsRole(YavscConstants.AdminGroupName)))
|
||||
{
|
||||
ModelState.AddModelError("ChannelName", "access post refused");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.ChatRoomAccess.Add(chatRoomAccess);
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (ChatRoomAccessExists(chatRoomAccess.ChannelName))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetChatRoomAccess", new { id = chatRoomAccess.ChannelName }, chatRoomAccess);
|
||||
}
|
||||
|
||||
// DELETE: api/ChatRoomAccessApi/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteChatRoomAccess([FromRoute] string id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
ChatRoomAccess chatRoomAccess = await _context.ChatRoomAccess.Include(acc => acc.Room).SingleAsync(m => m.ChannelName == id);
|
||||
if (chatRoomAccess == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var room = _context.ChatRoom.First(channel => channel.Name == chatRoomAccess.ChannelName );
|
||||
if (room == null || (uid != room.OwnerId && chatRoomAccess.UserId != uid && ! User.IsInMsRole(YavscConstants.AdminGroupName)))
|
||||
{
|
||||
ModelState.AddModelError("UserId", "access drop refused");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.ChatRoomAccess.Remove(chatRoomAccess);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(chatRoomAccess);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private bool ChatRoomAccessExists(string id)
|
||||
{
|
||||
return _context.ChatRoomAccess.Count(e => e.ChannelName == id) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
164
src/Yavsc.Api/Controllers/Relationship/ChatRoomApiController.cs
Normal file
164
src/Yavsc.Api/Controllers/Relationship/ChatRoomApiController.cs
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Chat;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/ChatRoomApi")]
|
||||
public class ChatRoomApiController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public ChatRoomApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/ChatRoomApi
|
||||
[HttpGet]
|
||||
public IEnumerable<ChatRoom> GetChatRoom()
|
||||
{
|
||||
return _context.ChatRoom;
|
||||
}
|
||||
|
||||
// GET: api/ChatRoomApi/5
|
||||
[HttpGet("{id}", Name = "GetChatRoom")]
|
||||
public async Task<IActionResult> GetChatRoom([FromRoute] string id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
ChatRoom chatRoom = await _context.ChatRoom.SingleAsync(m => m.Name == id);
|
||||
|
||||
if (chatRoom == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(chatRoom);
|
||||
}
|
||||
|
||||
// PUT: api/ChatRoomApi/5
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutChatRoom([FromRoute] string id, [FromBody] ChatRoom chatRoom)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (id != chatRoom.Name)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
if (User.GetUserId() != chatRoom.OwnerId )
|
||||
{
|
||||
return BadRequest(new {error = "OwnerId"});
|
||||
}
|
||||
|
||||
_context.Entry(chatRoom).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!ChatRoomExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
|
||||
// POST: api/ChatRoomApi
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostChatRoom([FromBody] ChatRoom chatRoom)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (User.GetUserId() != chatRoom.OwnerId )
|
||||
{
|
||||
return BadRequest(new {error = "OwnerId"});
|
||||
}
|
||||
|
||||
_context.ChatRoom.Add(chatRoom);
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (ChatRoomExists(chatRoom.Name))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetChatRoom", new { id = chatRoom.Name }, chatRoom);
|
||||
}
|
||||
|
||||
// DELETE: api/ChatRoomApi/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteChatRoom([FromRoute] string id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
ChatRoom chatRoom = await _context.ChatRoom.SingleAsync(m => m.Name == id);
|
||||
|
||||
|
||||
|
||||
if (chatRoom == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (User.GetUserId() != chatRoom.OwnerId )
|
||||
{
|
||||
if (!User.IsInMsRole(YavscConstants.AdminGroupName))
|
||||
return BadRequest(new {error = "OwnerId"});
|
||||
}
|
||||
|
||||
_context.ChatRoom.Remove(chatRoom);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(chatRoom);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private bool ChatRoomExists(string id)
|
||||
{
|
||||
return _context.ChatRoom.Count(e => e.Name == id) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
146
src/Yavsc.Api/Controllers/Relationship/CircleApiController.cs
Normal file
146
src/Yavsc.Api/Controllers/Relationship/CircleApiController.cs
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Relationship;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/cirle")]
|
||||
public class CircleApiController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public CircleApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/CircleApi
|
||||
[HttpGet]
|
||||
public IEnumerable<Circle> GetCircle()
|
||||
{
|
||||
return _context.Circle;
|
||||
}
|
||||
|
||||
// GET: api/CircleApi/5
|
||||
[HttpGet("{id}", Name = "GetCircle")]
|
||||
public async Task<IActionResult> GetCircle([FromRoute] long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
Circle circle = await _context.Circle.SingleAsync(m => m.Id == id);
|
||||
|
||||
if (circle == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(circle);
|
||||
}
|
||||
|
||||
// PUT: api/CircleApi/5
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutCircle([FromRoute] long id, [FromBody] Circle circle)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (id != circle.Id)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(circle).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!CircleExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
|
||||
// POST: api/CircleApi
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostCircle([FromBody] Circle circle)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.Circle.Add(circle);
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (CircleExists(circle.Id))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetCircle", new { id = circle.Id }, circle);
|
||||
}
|
||||
|
||||
// DELETE: api/CircleApi/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteCircle([FromRoute] long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
Circle circle = await _context.Circle.SingleAsync(m => m.Id == id);
|
||||
if (circle == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Circle.Remove(circle);
|
||||
await _context.SaveChangesAsync(User.GetUserId());
|
||||
|
||||
return Ok(circle);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private bool CircleExists(long id)
|
||||
{
|
||||
return _context.Circle.Count(e => e.Id == id) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
127
src/Yavsc.Api/Controllers/Relationship/ContactsApiController.cs
Normal file
127
src/Yavsc.Api/Controllers/Relationship/ContactsApiController.cs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Yavsc.Abstract.Identity;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/ContactsApi")]
|
||||
public class ContactsApiController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public ContactsApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/ContactsApi
|
||||
[HttpGet("{id}")]
|
||||
public ClientProviderInfo GetClientProviderInfo(string id)
|
||||
{
|
||||
return _context.ClientProviderInfo.FirstOrDefault(c=>c.UserId == id);
|
||||
}
|
||||
|
||||
// PUT: api/ContactsApi/5
|
||||
[HttpPut("{id}")]
|
||||
public IActionResult PutClientProviderInfo(string id, [FromBody] ClientProviderInfo clientProviderInfo)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (id != clientProviderInfo.UserId)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(clientProviderInfo).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!ClientProviderInfoExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
|
||||
// POST: api/ContactsApi
|
||||
[HttpPost]
|
||||
public IActionResult PostClientProviderInfo([FromBody] ClientProviderInfo clientProviderInfo)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.ClientProviderInfo.Add(clientProviderInfo);
|
||||
try
|
||||
{
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (ClientProviderInfoExists(clientProviderInfo.UserId))
|
||||
{
|
||||
return new StatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetClientProviderInfo", new { id = clientProviderInfo.UserId }, clientProviderInfo);
|
||||
}
|
||||
|
||||
// DELETE: api/ContactsApi/5
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult DeleteClientProviderInfo(string id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
ClientProviderInfo clientProviderInfo = _context.ClientProviderInfo.Single(m => m.UserId == id);
|
||||
if (clientProviderInfo == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.ClientProviderInfo.Remove(clientProviderInfo);
|
||||
_context.SaveChanges(User.GetUserId());
|
||||
|
||||
return Ok(clientProviderInfo);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private bool ClientProviderInfoExists(string id)
|
||||
{
|
||||
return _context.ClientProviderInfo.Count(e => e.UserId == id) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue