Skip to content

Commit

Permalink
Add partial implementation of private message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Feb 23, 2019
1 parent b91785d commit bf03395
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 3 deletions.
73 changes: 73 additions & 0 deletions Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using AutoMapper;
using CloudinaryDotNet;
using CloudinaryDotNet.Actions;
using Friendster.Controllers.Resources;
using Friendster.Data;
using Friendster.Helpers;
using Friendster.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Friendster.Controllers
{
[ServiceFilter(typeof(LogActivity))]
[Authorize]
[Route("api/users/{userId}/[controller]")]
[ApiController]
public class MessagesController : ControllerBase
{
private IFriendRepository _repo;
private IMapper _mapper;

public MessagesController(IFriendRepository repo, IMapper mapper)
{
_repo = repo;
_mapper = mapper;
}

[HttpGet("{messageId}", Name = "GetMessage")]
public async Task<IActionResult> GetMessage(int userId, int messageId)
{
int id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

if (id != userId)
{
return Unauthorized();
}

var message = await _repo.GetMessage(messageId);
var messageResource = _mapper.Map<Message, SendMessageResource>(message);
return Ok(messageResource);
}

//[HttpPost]
//public async Task<ActionResult> SendMessage(int userId, int recipientId, [FromForm]SendMessageResource sendMessageResource)
//{
// int id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

// if (id != userId)
// {
// return Unauthorized();
// }


//}

//[HttpDelete("{messageId}")]
//public async Task<IActionResult> DeleteMessage(int userId, int messageId)
//{
// int id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

// if (id != userId)
// {
// return Unauthorized();
// }


//}
}
}
2 changes: 1 addition & 1 deletion Controllers/PhotosController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace Friendster.Controllers
{
[Authorize]
[Route("api/users/{userId}/photos")]
[Route("api/users/{userId}/[controller]")]
[ApiController]
public class PhotosController : ControllerBase
{
Expand Down
13 changes: 13 additions & 0 deletions Controllers/Resources/SendMessageResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Friendster.Controllers.Resources
{
public class SendMessageResource
{
public string Content { get; set; }
public DateTime MessageSent { get; set; }
}
}
11 changes: 11 additions & 0 deletions Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class DataContext : DbContext
public DbSet<User> Users { get; set; }
public DbSet<Photo> Photos { get; set; }
public DbSet<Like> Likes { get; set; }
public DbSet<Message> Messages { get; set; }

public DataContext(DbContextOptions<DataContext> options) : base(options)
{
Expand All @@ -28,6 +29,16 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasOne(u => u.Liker).WithMany(u => u.Likees)
.HasForeignKey(u => u.LikerId)
.OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Message>()
.HasOne(u => u.Sender)
.WithMany(m => m.SentMessages)
.OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Message>()
.HasOne(u => u.Recipient)
.WithMany(m => m.ReceivedMessages)
.OnDelete(DeleteBehavior.Restrict);
}
}
}
16 changes: 15 additions & 1 deletion Data/FriendRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,26 @@ private async Task<IEnumerable<int>> GetUserLikes(int userId, bool getLikers)
return user.Likers
.Where(u => u.LikeeId == userId)
.Select(u => u.LikerId);

}

return user.Likees
.Where(u => u.LikerId == userId)
.Select(u => u.LikeeId);
}

public async Task<Message> GetMessage(int messageId)
{
return await _context.Messages.FirstOrDefaultAsync(m => m.Id == messageId);
}

public async Task<PagedList<Message>> GetMessages()
{
throw new NotImplementedException();
}

public async Task<IEnumerable<Message>> GetMessageThread(int userId, int recipientId)
{
throw new NotImplementedException();
}
}
}
4 changes: 3 additions & 1 deletion Data/IFriendRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface IFriendRepository
Task<Photo> GetPhoto(int photoId);
Task<Photo> GetMainPhoto(int userId);
Task<Like> GetLike(int userId, int recipientId);

Task<Message> GetMessage(int messageId);
Task<PagedList<Message>> GetMessages();
Task<IEnumerable<Message>> GetMessageThread(int userId, int recipientId);
}
}
179 changes: 179 additions & 0 deletions Migrations/20190222232957_AddMessageEntity.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bf03395

Please sign in to comment.