Files
aspnet-core-signalr-angular/Controllers/FeedsController.cs
2016-10-04 13:19:55 +03:00

68 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR.Infrastructure;
using LiveGameFeed.Hubs;
using LiveGameFeed.Data.Abstract;
using LiveGameFeed.Models;
using AutoMapper;
namespace LiveGameFeed.Controllers
{
[Route("api/[controller]")]
public class FeedsController : ApiHubController<Broadcaster>
{
IFeedRepository _feedRepository;
IMatchRepository _matchRepository;
public FeedsController(
IConnectionManager signalRConnectionManager,
IFeedRepository feedRepository,
IMatchRepository matchRepository)
: base(signalRConnectionManager)
{
_feedRepository = feedRepository;
_matchRepository = matchRepository;
}
// GET api/values
[HttpGet]
public IEnumerable<Feed> Get()
{
IEnumerable<Feed> _feeds = _feedRepository.GetAll();
return _feeds;
}
// GET api/feeds/5
[HttpGet("{id}")]
public Feed Get(int id)
{
return _feedRepository.GetSingle(id);
}
// POST api/feeds
[HttpPost]
public async void Post([FromBody]FeedViewModel feed)
{
Match _match = _matchRepository.GetSingle(feed.MatchId);
Feed _matchFeed = new Feed()
{
Description = feed.Description,
CreatedAt = feed.CreatedAt,
MatchId = feed.MatchId
};
_match.Feeds.Add(_matchFeed);
_matchRepository.Commit();
FeedViewModel _feedVM = Mapper.Map<Feed, FeedViewModel>(_matchFeed);
await Clients.Group(feed.MatchId.ToString()).addFeed(_feedVM);
}
}
}