Un-asynced

This commit is contained in:
Jack Hadrill 2020-09-21 22:48:44 +01:00
parent 0c29d79c2b
commit d44c01d629
7 changed files with 148 additions and 66 deletions

View File

@ -22,24 +22,24 @@ namespace VCinemaApi.Hubs
public override async Task OnConnectedAsync() public override async Task OnConnectedAsync()
{ {
await _watcherRepository.CreateWatcher(Context.ConnectionId); _watcherRepository.AddWatcher(Context.ConnectionId);
await base.OnConnectedAsync(); await base.OnConnectedAsync();
} }
public override async Task OnDisconnectedAsync(Exception exception) public override async Task OnDisconnectedAsync(Exception exception)
{ {
await _watcherRepository.DeleteWatcher(Context.ConnectionId); _watcherRepository.DeleteWatcher(Context.ConnectionId);
await base.OnDisconnectedAsync(exception); await base.OnDisconnectedAsync(exception);
} }
public async Task<IEnumerable<Screen>> GetScreens() public IEnumerable<Screen> GetScreens()
{ {
return await _screenRepository.GetScreens(); return _screenRepository.GetScreens();
} }
public async Task<Screen> GetScreenById(int screenId) public Screen GetScreenById(int screenId)
{ {
return await _screenRepository.GetScreenById(screenId); return _screenRepository.GetScreenById(screenId);
} }
} }
} }

View File

@ -6,7 +6,9 @@ namespace VCinemaApi.Repositories
{ {
public interface IScreenRepository public interface IScreenRepository
{ {
Task<List<Screen>> GetScreens(); IEnumerable<Screen> GetScreens();
Task<Screen> GetScreenById(int screenId); Screen GetScreenById(int screenId);
Screen AddScreen(string screenName);
void DeleteScreen(int screenId);
} }
} }

View File

@ -6,9 +6,11 @@ namespace VCinemaApi.Repositories
{ {
public interface IWatcherRepository public interface IWatcherRepository
{ {
Task<List<Watcher>> GetWatchersByScreenId(int screenId); IEnumerable<Watcher> GetWatchersByScreenId(int screenId);
Task<Watcher> GetWatcherByConnectionId(string connectionId); Watcher GetWatcherByConnectionId(string connectionId);
Task CreateWatcher(string connectionId); Watcher AddWatcher(string connectionId);
Task DeleteWatcher(string connectionId); Watcher AddWatcher(string connectionId, int screenId);
Watcher SetWatcherName(string connectionId, string name);
void DeleteWatcher(string connectionId);
} }
} }

View File

@ -7,9 +7,9 @@ namespace VCinemaApi.Repositories
{ {
public class MockScreenRepository : IScreenRepository public class MockScreenRepository : IScreenRepository
{ {
public Task<List<Screen>> GetScreens() public IEnumerable<Screen> GetScreens()
{ {
var screens = new List<Screen> return new List<Screen>
{ {
new Screen { new Screen {
ScreenId = 1, ScreenId = 1,
@ -28,12 +28,11 @@ namespace VCinemaApi.Repositories
PlayOffset = 69 PlayOffset = 69
} }
}; };
return Task.FromResult<List<Screen>>(screens);
} }
public Task<Screen> GetScreenById(int screenId) public Screen GetScreenById(int screenId)
{ {
var screen = new Screen return new Screen
{ {
ScreenId = 1, ScreenId = 1,
Name = "Kirby's screen", Name = "Kirby's screen",
@ -42,7 +41,24 @@ namespace VCinemaApi.Repositories
PlayState = true, PlayState = true,
PlayOffset = 1337 PlayOffset = 1337
}; };
return Task.FromResult<Screen>(screen); }
public Screen AddScreen(string screenName)
{
return new Screen
{
ScreenId = 1,
Name = "Kirby's screen",
Source = "https://vcinema.b-cdn.net/shrek.mp4",
PlayStateUpdated = DateTime.UtcNow,
PlayState = true,
PlayOffset = 1337
};
}
public void DeleteScreen(int screenId)
{
} }
} }
} }

View File

@ -7,52 +7,69 @@ namespace VCinemaApi.Repositories
{ {
public class MockWatcherRepository : IWatcherRepository public class MockWatcherRepository : IWatcherRepository
{ {
public Task<List<Watcher>> GetWatchersByScreenId(int screenId) public IEnumerable<Watcher> GetWatchersByScreenId(int screenId)
{ {
var watchers = new List<Watcher> var screen = new Screen
{
ScreenId = 1,
Name = "Kirby's screen",
Source = "https://vcinema.b-cdn.net/shrek.mp4",
PlayStateUpdated = new DateTime(2020, 9, 21, 21, 02, 57),
PlayState = true,
PlayOffset = 1337
};
return new List<Watcher>
{ {
new Watcher new Watcher
{ {
WatcherId = 1, WatcherId = 1,
ConnectionId = "dJSbEc73n6YjGIhj-SZz1Q", ConnectionId = "dJSbEc73n6YjGIhj-SZz1Q",
Name = "Kirby", Name = "Kirby",
Screen = new Screen { Screen = screen
ScreenId = 1,
Name = "Kirby's screen",
Source = "https://vcinema.b-cdn.net/shrek.mp4",
PlayStateUpdated = new DateTime(2020, 9, 21, 21, 02, 57),
PlayState = true,
PlayOffset = 1337
}
}, },
new Watcher new Watcher
{ {
WatcherId = 2, WatcherId = 2,
ConnectionId = "rNA9Jn7ytYPzQfFJ-j3NBa", ConnectionId = "rNA9Jn7ytYPzQfFJ-j3NBa",
Name = "Sid", Name = "Jack",
Screen = new Screen { Screen = screen
ScreenId = 2,
Name = "Sid's screen",
Source = "https://vcinema.b-cdn.net/weeb.mp4",
PlayStateUpdated = new DateTime(2020, 9, 21, 21, 03, 22),
PlayState = true,
PlayOffset = 69
}
} }
}; };
return Task.FromResult<List<Watcher>>(watchers);
} }
public Task<Watcher> GetWatcherByConnectionId(string connectionId) public Watcher GetWatcherByConnectionId(string connectionId)
{ {
var watcher = new Watcher return new Watcher
{ {
WatcherId = 1, WatcherId = 1,
ConnectionId = "dJSbEc73n6YjGIhj-SZz1Q", ConnectionId = "dJSbEc73n6YjGIhj-SZz1Q",
Name = "Kirby", Name = "Kirby",
Screen = null
};
}
public Watcher AddWatcher(string connectionId)
{
return new Watcher
{
WatcherId = 1,
ConnectionId = connectionId,
Name = "Kirby",
Screen = null
};
}
public Watcher AddWatcher(string connectionId, int screenId)
{
return new Watcher
{
WatcherId = 1,
ConnectionId = connectionId,
Name = "Kirby",
Screen = new Screen Screen = new Screen
{ {
ScreenId = 1, ScreenId = screenId,
Name = "Kirby's screen", Name = "Kirby's screen",
Source = "https://vcinema.b-cdn.net/shrek.mp4", Source = "https://vcinema.b-cdn.net/shrek.mp4",
PlayStateUpdated = new DateTime(2020, 9, 21, 21, 02, 57), PlayStateUpdated = new DateTime(2020, 9, 21, 21, 02, 57),
@ -60,17 +77,22 @@ namespace VCinemaApi.Repositories
PlayOffset = 1337 PlayOffset = 1337
} }
}; };
return Task.FromResult<Watcher>(watcher);
} }
public Task CreateWatcher(string connectionId) public Watcher SetWatcherName(string connectionId, string name)
{ {
throw new NotImplementedException(); return new Watcher
{
WatcherId = 1,
ConnectionId = connectionId,
Name = "Kirby",
Screen = null
};
} }
public Task DeleteWatcher(string connectionId) public void DeleteWatcher(string connectionId)
{ {
throw new NotImplementedException();
} }
} }
} }

View File

@ -1,6 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Linq;
using Microsoft.EntityFrameworkCore;
using VCinemaApi.Models; using VCinemaApi.Models;
namespace VCinemaApi.Repositories namespace VCinemaApi.Repositories
@ -14,14 +13,34 @@ namespace VCinemaApi.Repositories
_context = context; _context = context;
} }
public Task<List<Screen>> GetScreens() public IEnumerable<Screen> GetScreens()
{ {
return _context.Screens.ToListAsync(); return _context.Screens.ToList();
} }
public Task<Screen> GetScreenById(int screenId) public Screen GetScreenById(int screenId)
{ {
return _context.Screens.SingleOrDefaultAsync(screen => screen.ScreenId == screenId); return _context.Screens.Find(screenId);
}
public Screen AddScreen(string screenName)
{
var screen = new Screen
{
Name = screenName
};
_context.Screens.Add(screen);
_context.SaveChanges();
return screen;
}
public void DeleteScreen(int screenId)
{
var screen = GetScreenById(screenId);
_context.Screens.Remove(screen);
_context.SaveChanges();
} }
} }
} }

View File

@ -1,7 +1,5 @@
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using VCinemaApi.Models; using VCinemaApi.Models;
namespace VCinemaApi.Repositories namespace VCinemaApi.Repositories
@ -15,32 +13,55 @@ namespace VCinemaApi.Repositories
_context = context; _context = context;
} }
public Task<List<Watcher>> GetWatchersByScreenId(int screenId) public IEnumerable<Watcher> GetWatchersByScreenId(int screenId)
{ {
throw new NotImplementedException(); return _context.Watchers.Where(watcher => watcher.Screen.ScreenId == screenId);
} }
public Task<Watcher> GetWatcherByConnectionId(string connectionId) public Watcher GetWatcherByConnectionId(string connectionId)
{ {
return _context.Watchers.SingleOrDefaultAsync(watcher => watcher.ConnectionId == connectionId); return _context.Watchers.SingleOrDefault(watcher => watcher.ConnectionId == connectionId);
} }
public async Task CreateWatcher(string connectionId) public Watcher AddWatcher(string connectionId)
{ {
var watcher = new Watcher() var watcher = new Watcher()
{ {
ConnectionId = connectionId, ConnectionId = connectionId,
}; };
await _context.Watchers.AddAsync(watcher); _context.Watchers.Add(watcher);
await _context.SaveChangesAsync(); _context.SaveChanges();
return watcher;
} }
public async Task DeleteWatcher(string connectionId) public Watcher AddWatcher(string connectionId, int screenId)
{ {
var watcher = await GetWatcherByConnectionId(connectionId); var watcher = _context.Watchers.SingleOrDefault(watcher => watcher.ConnectionId == connectionId);
var screen = _context.Screens.Find(screenId);
watcher.Screen = screen;
_context.SaveChanges();
return watcher;
}
public Watcher SetWatcherName(string connectionId, string name)
{
var watcher = _context.Watchers.SingleOrDefault(watcher => watcher.ConnectionId == connectionId);
watcher.Name = name;
_context.SaveChanges();
return watcher;
}
public void DeleteWatcher(string connectionId)
{
var watcher = GetWatcherByConnectionId(connectionId);
_context.Watchers.Remove(watcher); _context.Watchers.Remove(watcher);
await _context.SaveChangesAsync(); _context.SaveChanges();
} }
} }
} }