Skip to content
Snippets Groups Projects
Commit e681edef authored by Lucca Santangelo Dodera's avatar Lucca Santangelo Dodera
Browse files

Merge branch 'feature/send-message' into 'master'

Feature/send message

See merge request !3
parents 171024d9 15ddd664
No related branches found
No related tags found
1 merge request!3Feature/send message
Showing with 245 additions and 0 deletions
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);
}
}
}
......@@ -14,6 +14,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Tsi1.Api.Infrastructure;
......@@ -21,6 +22,7 @@ using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.BusinessLayer.Services;
using Tsi1.DataLayer;
using Tsi1.DataLayer.MongoDbConfiguration;
namespace Tsi1.Api
{
......@@ -39,6 +41,15 @@ namespace Tsi1.Api
services.AddControllers();
services.AddDbContext<Tsi1Context>(x => x.UseNpgsql(Configuration.GetConnectionString("PostgreSql")));
services.Configure<Tsi1DatabaseSettings>(
Configuration.GetSection(nameof(Tsi1DatabaseSettings)));
services.AddSingleton<ITsi1DatabaseSettings>(sp =>
sp.GetRequiredService<IOptions<Tsi1DatabaseSettings>>().Value);
services.AddSingleton<IMessageService, MessageService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserTypeService, UserTypeService>();
services.AddScoped<ICourseService, CourseService>();
......
......@@ -2,6 +2,11 @@
"ConnectionStrings": {
"PostgreSql": "Host=localhost;Database=tsi1;Username=postgres;Password=111111"
},
"Tsi1DatabaseSettings": {
"Tsi1CollectionName": "Messages",
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "Tsi1Db"
},
"jwtTokenConfig": {
"secret": "1234567890123456789",
"issuer": "https://localhost:44363",
......
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
namespace Tsi1.BusinessLayer.Dtos
{
public class MessageCreateDto
{
[JsonIgnore]
public int SenderId { get; set; }
public int ReceiverId { get; set; }
public string Content { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class MessagePreviewDto
{
public string Id { get; set; }
public int SenderId { get; set; }
public int ReceiverId { get; set; }
public string Content { get; set; }
public string Date { get; set; }
}
}
......@@ -18,6 +18,8 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<Post, PostPreviewDto>();
CreateMap<PostMessage, PostMessageCreateDto>();
CreateMap<PostMessage, PostMessagePreviewDto>();
CreateMap<Message, MessagePreviewDto>();
CreateMap<Message, MessageCreateDto>();
CreateMap<ForumCreateDto, Forum>();
CreateMap<ForumPreviewDto, Forum>();
......@@ -25,6 +27,8 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<PostPreviewDto, Post>();
CreateMap<PostMessageCreateDto, PostMessage>();
CreateMap<PostMessagePreviewDto, PostMessage>();
CreateMap<MessagePreviewDto, Message>();
CreateMap<MessageCreateDto, Message>();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
namespace Tsi1.BusinessLayer.Interfaces
{
public interface IMessageService
{
Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId);
Task<ServiceResult<MessagePreviewDto>> Send(MessageCreateDto newMessage);
}
}
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;
}
}
}
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.DataLayer.Entities
{
public class Message
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public int SenderId { get; set; }
public int ReceiverId { get; set; }
public string Content { get; set; }
public DateTime Date { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.DataLayer.MongoDbConfiguration
{
public class Tsi1DatabaseSettings : ITsi1DatabaseSettings
{
public string Tsi1CollectionName { get; set; }
public string ConnectionString { get; set; }
public string DatabaseName { get; set; }
}
public interface ITsi1DatabaseSettings
{
public string Tsi1CollectionName { get; set; }
public string ConnectionString { get; set; }
public string DatabaseName { get; set; }
}
}
......@@ -22,6 +22,7 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" 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" />
</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