diff --git a/VCinema/Hubs/VCinemaHub.cs b/VCinema/Hubs/VCinemaHub.cs index d59c1e0..f48f566 100644 --- a/VCinema/Hubs/VCinemaHub.cs +++ b/VCinema/Hubs/VCinemaHub.cs @@ -22,24 +22,24 @@ namespace VCinemaApi.Hubs public override async Task OnConnectedAsync() { - await _watcherRepository.CreateWatcher(Context.ConnectionId); + _watcherRepository.AddWatcher(Context.ConnectionId); await base.OnConnectedAsync(); } public override async Task OnDisconnectedAsync(Exception exception) { - await _watcherRepository.DeleteWatcher(Context.ConnectionId); + _watcherRepository.DeleteWatcher(Context.ConnectionId); await base.OnDisconnectedAsync(exception); } - public async Task> GetScreens() + public IEnumerable GetScreens() { - return await _screenRepository.GetScreens(); + return _screenRepository.GetScreens(); } - public async Task GetScreenById(int screenId) + public Screen GetScreenById(int screenId) { - return await _screenRepository.GetScreenById(screenId); + return _screenRepository.GetScreenById(screenId); } } } diff --git a/VCinema/Repositories/IScreenRepository.cs b/VCinema/Repositories/IScreenRepository.cs index f77ba14..e7d49ca 100644 --- a/VCinema/Repositories/IScreenRepository.cs +++ b/VCinema/Repositories/IScreenRepository.cs @@ -6,7 +6,9 @@ namespace VCinemaApi.Repositories { public interface IScreenRepository { - Task> GetScreens(); - Task GetScreenById(int screenId); + IEnumerable GetScreens(); + Screen GetScreenById(int screenId); + Screen AddScreen(string screenName); + void DeleteScreen(int screenId); } } diff --git a/VCinema/Repositories/IWatcherRepository.cs b/VCinema/Repositories/IWatcherRepository.cs index 8a60330..cc82511 100644 --- a/VCinema/Repositories/IWatcherRepository.cs +++ b/VCinema/Repositories/IWatcherRepository.cs @@ -6,9 +6,11 @@ namespace VCinemaApi.Repositories { public interface IWatcherRepository { - Task> GetWatchersByScreenId(int screenId); - Task GetWatcherByConnectionId(string connectionId); - Task CreateWatcher(string connectionId); - Task DeleteWatcher(string connectionId); + IEnumerable GetWatchersByScreenId(int screenId); + Watcher GetWatcherByConnectionId(string connectionId); + Watcher AddWatcher(string connectionId); + Watcher AddWatcher(string connectionId, int screenId); + Watcher SetWatcherName(string connectionId, string name); + void DeleteWatcher(string connectionId); } } diff --git a/VCinema/Repositories/MockScreenRepository.cs b/VCinema/Repositories/MockScreenRepository.cs index acf61a8..e3fd6f9 100644 --- a/VCinema/Repositories/MockScreenRepository.cs +++ b/VCinema/Repositories/MockScreenRepository.cs @@ -7,9 +7,9 @@ namespace VCinemaApi.Repositories { public class MockScreenRepository : IScreenRepository { - public Task> GetScreens() + public IEnumerable GetScreens() { - var screens = new List + return new List { new Screen { ScreenId = 1, @@ -28,12 +28,11 @@ namespace VCinemaApi.Repositories PlayOffset = 69 } }; - return Task.FromResult>(screens); } - public Task GetScreenById(int screenId) + public Screen GetScreenById(int screenId) { - var screen = new Screen + return new Screen { ScreenId = 1, Name = "Kirby's screen", @@ -42,7 +41,24 @@ namespace VCinemaApi.Repositories PlayState = true, PlayOffset = 1337 }; - return Task.FromResult(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) + { + } } } diff --git a/VCinema/Repositories/MockWatcherRepository.cs b/VCinema/Repositories/MockWatcherRepository.cs index d6cc877..998ae7a 100644 --- a/VCinema/Repositories/MockWatcherRepository.cs +++ b/VCinema/Repositories/MockWatcherRepository.cs @@ -7,52 +7,69 @@ namespace VCinemaApi.Repositories { public class MockWatcherRepository : IWatcherRepository { - public Task> GetWatchersByScreenId(int screenId) + public IEnumerable GetWatchersByScreenId(int screenId) { - var watchers = new List + 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 { new Watcher { WatcherId = 1, ConnectionId = "dJSbEc73n6YjGIhj-SZz1Q", Name = "Kirby", - 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 - } + Screen = screen }, new Watcher { WatcherId = 2, ConnectionId = "rNA9Jn7ytYPzQfFJ-j3NBa", - Name = "Sid", - Screen = new 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 - } + Name = "Jack", + Screen = screen } }; - return Task.FromResult>(watchers); } - public Task GetWatcherByConnectionId(string connectionId) + public Watcher GetWatcherByConnectionId(string connectionId) { - var watcher = new Watcher + return new Watcher { WatcherId = 1, ConnectionId = "dJSbEc73n6YjGIhj-SZz1Q", 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 { - ScreenId = 1, + ScreenId = screenId, Name = "Kirby's screen", Source = "https://vcinema.b-cdn.net/shrek.mp4", PlayStateUpdated = new DateTime(2020, 9, 21, 21, 02, 57), @@ -60,17 +77,22 @@ namespace VCinemaApi.Repositories PlayOffset = 1337 } }; - return Task.FromResult(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(); + } } } diff --git a/VCinema/Repositories/ScreenRepository.cs b/VCinema/Repositories/ScreenRepository.cs index cce58e1..08e869d 100644 --- a/VCinema/Repositories/ScreenRepository.cs +++ b/VCinema/Repositories/ScreenRepository.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; +using System.Linq; using VCinemaApi.Models; namespace VCinemaApi.Repositories @@ -14,14 +13,34 @@ namespace VCinemaApi.Repositories _context = context; } - public Task> GetScreens() + public IEnumerable GetScreens() { - return _context.Screens.ToListAsync(); + return _context.Screens.ToList(); } - public Task 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(); } } } diff --git a/VCinema/Repositories/WatcherRepository.cs b/VCinema/Repositories/WatcherRepository.cs index 7193bb6..88ffad8 100644 --- a/VCinema/Repositories/WatcherRepository.cs +++ b/VCinema/Repositories/WatcherRepository.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; using VCinemaApi.Models; namespace VCinemaApi.Repositories @@ -15,32 +13,55 @@ namespace VCinemaApi.Repositories _context = context; } - public Task> GetWatchersByScreenId(int screenId) + public IEnumerable GetWatchersByScreenId(int screenId) { - throw new NotImplementedException(); + return _context.Watchers.Where(watcher => watcher.Screen.ScreenId == screenId); } - public Task 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() { ConnectionId = connectionId, }; - await _context.Watchers.AddAsync(watcher); - await _context.SaveChangesAsync(); + _context.Watchers.Add(watcher); + _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); - await _context.SaveChangesAsync(); + _context.SaveChanges(); } } }