Skip to content

Commit

Permalink
Merge pull request #8 from comitivaesperanca/dev
Browse files Browse the repository at this point in the history
[FEATURE] Criando funcionalidade de salvar sugestão de sentimento para notícias
  • Loading branch information
FelipeGaleao authored May 15, 2023
2 parents 2534976 + 39d9a8e commit 246e6ba
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using ComitivaEsperanca.API.Domain.Entities;

namespace ComitivaEsperanca.API.Domain.Interfaces.Repositories.Entities
{
public interface IClassifiedNewsRepository : IBaseRepository<ClassifiedNews>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace ComitivaEsperanca.API.Domain.Interfaces.UnitOfWork
public interface IUnitOfWork : IDisposable
{
INewsRepository NewsRepository { get;}
IClassifiedNewsRepository ClassifiedNewsRepository { get;}
int Commit();
dynamic GetContext();
}
Expand Down
24 changes: 24 additions & 0 deletions ComitivaEsperanca.API.Service/Services/NewsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ public ResponseDTO<News> UpdateNews(NewsDTO newsDTO)
}
}

public ResponseDTO<ClassifiedNews> SaveSuggestedFeeling(Guid newsId, string feeling)
{
try
{
var news = _unitOfWork.NewsRepository.Get(x => x.Id == newsId);
if (news == null)
return new ResponseDTO<ClassifiedNews>(StatusCodes.Status404NotFound);

var classifiedNews = new ClassifiedNews()
{
NewsId = newsId,
SuggestedFeeling = feeling
};
_unitOfWork.ClassifiedNewsRepository.Add(classifiedNews);
if (_unitOfWork.Commit() > 0)
return new ResponseDTO<ClassifiedNews>(StatusCodes.Status201Created, classifiedNews);
return new ResponseDTO<ClassifiedNews>(StatusCodes.Status400BadRequest);
}
catch (Exception ex)
{
return new ResponseDTO<ClassifiedNews>(StatusCodes.Status500InternalServerError, ex.Message);
}
}

public int GetTotalNews()
{
return _unitOfWork.NewsRepository.GetAll().Count();
Expand Down
11 changes: 11 additions & 0 deletions ComitivaEsperanca.API/Controllers/NewsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,16 @@ public string GetMostFrequentSentimentOnWeek()
return _newsService.GetMostFrequentSentimentOnWeek();
}

[HttpPost("SaveSuggestedSentiment")]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(ResponseDTO<ClassifiedNews>))]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ResponseDTO<ClassifiedNews>))]
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ResponseDTO<ClassifiedNews>))]
public IActionResult Create(Guid newsId, string sentiment)
{
var response = _newsService.SaveSuggestedFeeling(newsId, sentiment);
return StatusCode(response.StatusCode, response.Entity);
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using ComitivaEsperanca.API.Data.Context;
using ComitivaEsperanca.API.Domain.Entities;
using ComitivaEsperanca.API.Domain.Interfaces.Repositories.Entities;

namespace ComitivaEsperanca.API.Data.Repositories.Entities
{
public class ClassifiedNewsRepository : BaseRepository<ClassifiedNews>, IClassifiedNewsRepository
{
public ClassifiedNewsRepository(CoreContext context) : base(context)
{

}
}
}
2 changes: 2 additions & 0 deletions ComitivaEsperanca.Data/UnitOfWork/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ public class UnitOfWork : IUnitOfWork
private bool disposed = false;

public INewsRepository NewsRepository { get; set; }
public IClassifiedNewsRepository ClassifiedNewsRepository { get; set; }

public UnitOfWork(CoreContext context)
{
_context = context;
this.NewsRepository = new NewsRepository(_context);
this.ClassifiedNewsRepository = new ClassifiedNewsRepository(_context);
}

protected virtual void Dispose(bool disposing)
Expand Down

0 comments on commit 246e6ba

Please sign in to comment.