Skip to content
Snippets Groups Projects
Commit 836d9df2 authored by esantangelo's avatar esantangelo
Browse files

add message controller

parent 9fd19484
No related branches found
No related tags found
1 merge request!3Feature/send message
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
namespace Tsi1.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MessageController : ControllerBase
{
private readonly IMessageService _messageService;
public MessageController(IMessageService messageService)
{
_messageService = messageService;
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpGet("GetMessages/{receiverId}")]
public async Task<ActionResult> GetMessages(int receiverId)
{
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
var result = await _messageService.GetMessages(userId, receiverId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpPost("Send")]
public async Task<ActionResult> Send(MessageCreateDto newMessage)
{
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
newMessage.SenderId = userId;
var result = await _messageService.Send(newMessage);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
}
}
...@@ -42,12 +42,14 @@ namespace Tsi1.Api ...@@ -42,12 +42,14 @@ namespace Tsi1.Api
services.AddDbContext<Tsi1Context>(x => x.UseNpgsql(Configuration.GetConnectionString("PostgreSql"))); services.AddDbContext<Tsi1Context>(x => x.UseNpgsql(Configuration.GetConnectionString("PostgreSql")));
services.Configure<ITsi1DatabaseSettings>( services.Configure<Tsi1DatabaseSettings>(
Configuration.GetSection(nameof(Tsi1DatabaseSettings))); Configuration.GetSection(nameof(Tsi1DatabaseSettings)));
services.AddSingleton<Tsi1DatabaseSettings>(sp => services.AddSingleton<ITsi1DatabaseSettings>(sp =>
sp.GetRequiredService<IOptions<Tsi1DatabaseSettings>>().Value); sp.GetRequiredService<IOptions<Tsi1DatabaseSettings>>().Value);
services.AddSingleton<IMessageService, MessageService>();
services.AddScoped<IUserService, UserService>(); services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserTypeService, UserTypeService>(); services.AddScoped<IUserTypeService, UserTypeService>();
services.AddScoped<ICourseService, CourseService>(); services.AddScoped<ICourseService, CourseService>();
......
...@@ -18,6 +18,8 @@ namespace Tsi1.BusinessLayer.Helpers ...@@ -18,6 +18,8 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<Post, PostPreviewDto>(); CreateMap<Post, PostPreviewDto>();
CreateMap<PostMessage, PostMessageCreateDto>(); CreateMap<PostMessage, PostMessageCreateDto>();
CreateMap<PostMessage, PostMessagePreviewDto>(); CreateMap<PostMessage, PostMessagePreviewDto>();
CreateMap<Message, MessagePreviewDto>();
CreateMap<Message, MessageCreateDto>();
CreateMap<ForumCreateDto, Forum>(); CreateMap<ForumCreateDto, Forum>();
CreateMap<ForumPreviewDto, Forum>(); CreateMap<ForumPreviewDto, Forum>();
...@@ -25,6 +27,8 @@ namespace Tsi1.BusinessLayer.Helpers ...@@ -25,6 +27,8 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<PostPreviewDto, Post>(); CreateMap<PostPreviewDto, Post>();
CreateMap<PostMessageCreateDto, PostMessage>(); CreateMap<PostMessageCreateDto, PostMessage>();
CreateMap<PostMessagePreviewDto, PostMessage>(); CreateMap<PostMessagePreviewDto, PostMessage>();
CreateMap<MessagePreviewDto, Message>();
CreateMap<MessageCreateDto, Message>();
} }
} }
} }
...@@ -2,25 +2,16 @@ ...@@ -2,25 +2,16 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers; using Tsi1.BusinessLayer.Helpers;
namespace Tsi1.BusinessLayer.Interfaces namespace Tsi1.BusinessLayer.Interfaces
{ {
public interface IMessageService public interface IMessageService
{ {
Task<ServiceResult<List<ForumPreviewDto>>> GetForums(int courseId); Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId);
Task<ServiceResult<Forum>> Create(ForumCreateDto newForum); Task<ServiceResult<MessagePreviewDto>> Send(MessageCreateDto newMessage);
Task<ServiceResult<Forum>> Delete(int forumId);
Task<ServiceResult<>>
List<Message> Get();
Message Get(string id);
Message Create(Message message);
void Update(string id, Message messageIn);
void Remove(Message messageIn);
void Remove(string id);
} }
} }
using AutoMapper;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer.Entities;
using Tsi1.DataLayer.MongoDbConfiguration;
namespace Tsi1.BusinessLayer.Services
{
public class MessageService : IMessageService
{
private readonly IMapper _mapper;
private readonly IMongoCollection<Message> _messages;
public MessageService(ITsi1DatabaseSettings settings, IMapper mapper)
{
var client = new MongoClient(settings.ConnectionString);
var database = client.GetDatabase(settings.DatabaseName);
_messages = database.GetCollection<Message>(settings.Tsi1CollectionName);
_mapper = mapper;
}
public async Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId)
{
var result = new ServiceResult<List<MessagePreviewDto>>();
var sort = Builders<Message>.Sort.Ascending("Date");
var messages = await _messages.Find(m => (m.SenderId == userId && m.ReceiverId == otherUserId)
|| (m.SenderId == otherUserId && m.ReceiverId == userId))
.Sort(sort)
.ToListAsync();
var messagesDto = _mapper.Map<List<MessagePreviewDto>>(messages);
result.Data = messagesDto;
return result;
}
public async Task<ServiceResult<MessagePreviewDto>> Send(MessageCreateDto newMessage)
{
var result = new ServiceResult<MessagePreviewDto>();
var message = _mapper.Map<Message>(newMessage);
message.Date = DateTime.Now;
await _messages.InsertOneAsync(message);
var messageDto = _mapper.Map<MessagePreviewDto>(message);
result.Data = messageDto;
return result;
}
}
}
...@@ -18,7 +18,7 @@ namespace Tsi1.DataLayer.Entities ...@@ -18,7 +18,7 @@ namespace Tsi1.DataLayer.Entities
public string Content { get; set; } public string Content { get; set; }
public string Date { get; set; } public DateTime Date { get; set; }
} }
} }
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.9" /> <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.9" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.9" />
<PackageReference Include="MongoDB.Driver" Version="2.11.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
</ItemGroup> </ItemGroup>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment