From 238e42f28c505b02a05de754d1634dcadacea264 Mon Sep 17 00:00:00 2001 From: Jack Hadrill Date: Mon, 21 Sep 2020 23:42:17 +0100 Subject: [PATCH] Update transactions to match design --- VCinema/Hubs/IVCinemaHub.cs | 6 ++--- VCinema/Hubs/VCinemaHub.cs | 27 ++++++++++--------- VCinema/Models/Screen.cs | 1 - VCinema/Models/Watcher.cs | 4 +-- VCinema/Repositories/IScreenRepository.cs | 6 ++--- VCinema/Repositories/IWatcherRepository.cs | 7 ++--- VCinema/Repositories/MockScreenRepository.cs | 12 ++++----- VCinema/Repositories/MockWatcherRepository.cs | 15 +++++++++-- VCinema/Repositories/ScreenRepository.cs | 11 ++++---- VCinema/Repositories/WatcherRepository.cs | 22 ++++++++++----- 10 files changed, 66 insertions(+), 45 deletions(-) diff --git a/VCinema/Hubs/IVCinemaHub.cs b/VCinema/Hubs/IVCinemaHub.cs index 66d2f45..e585bcf 100644 --- a/VCinema/Hubs/IVCinemaHub.cs +++ b/VCinema/Hubs/IVCinemaHub.cs @@ -4,12 +4,12 @@ namespace VCinemaApi.Hubs { public interface IVCinemaHub { - public Screen CreateScreen(string name); + public Screen CreateScreen(string name, string source); public void DeleteScreen(int screenId); public Screen WatchScreen(int screenId); public void LeaveScreen(); - public void SetWatcherName(string name); - public void SetPlayState(bool playing); + public Watcher SetWatcherName(string name); + public Screen SetPlayState(bool playing); public void SetPlayState(bool playing, int playOffset); } } diff --git a/VCinema/Hubs/VCinemaHub.cs b/VCinema/Hubs/VCinemaHub.cs index 0143941..4caa5e4 100644 --- a/VCinema/Hubs/VCinemaHub.cs +++ b/VCinema/Hubs/VCinemaHub.cs @@ -26,41 +26,42 @@ namespace VCinemaApi.Hubs public override async Task OnDisconnectedAsync(Exception exception) { - _watcherRepository.DeleteWatcher(Context.ConnectionId); + _watcherRepository.DeleteWatcherByConnectionId(Context.ConnectionId); await base.OnDisconnectedAsync(exception); } - public Screen CreateScreen(string name) + public Screen CreateScreen(string name, string source) { - throw new NotImplementedException(); + return _screenRepository.AddScreen(name, source); } public void DeleteScreen(int screenId) { - throw new NotImplementedException(); + _screenRepository.DeleteScreenById(screenId); } public Screen WatchScreen(int screenId) { - throw new NotImplementedException(); + var watcher = _watcherRepository.SetWatcherScreenByConnectionId(Context.ConnectionId, screenId); + return watcher.Screen; } public void LeaveScreen() { - throw new NotImplementedException(); + _watcherRepository.UnsetWatcherScreenByConnectionId(Context.ConnectionId); } - public void SetWatcherName(string name) + public Watcher SetWatcherName(string name) + { + return _watcherRepository.SetWatcherNameByConnectionId(Context.ConnectionId, name); + } + + public Screen SetPlayState(bool playing) { throw new NotImplementedException(); } - public void SetPlayState(bool playing) - { - throw new NotImplementedException(); - } - - public void SetPlayState(bool playing, int playOffset) + public Screen SetPlayState(bool playing, int playOffset) { throw new NotImplementedException(); } diff --git a/VCinema/Models/Screen.cs b/VCinema/Models/Screen.cs index d9979a0..891a84e 100644 --- a/VCinema/Models/Screen.cs +++ b/VCinema/Models/Screen.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; namespace VCinemaApi.Models { diff --git a/VCinema/Models/Watcher.cs b/VCinema/Models/Watcher.cs index 3d53711..4b6cc02 100644 --- a/VCinema/Models/Watcher.cs +++ b/VCinema/Models/Watcher.cs @@ -1,6 +1,4 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; namespace VCinemaApi.Models { diff --git a/VCinema/Repositories/IScreenRepository.cs b/VCinema/Repositories/IScreenRepository.cs index e7d49ca..21a5136 100644 --- a/VCinema/Repositories/IScreenRepository.cs +++ b/VCinema/Repositories/IScreenRepository.cs @@ -7,8 +7,8 @@ namespace VCinemaApi.Repositories public interface IScreenRepository { IEnumerable GetScreens(); - Screen GetScreenById(int screenId); - Screen AddScreen(string screenName); - void DeleteScreen(int screenId); + Screen GetScreenById(int id); + Screen AddScreen(string name, string source); + void DeleteScreenById(int id); } } diff --git a/VCinema/Repositories/IWatcherRepository.cs b/VCinema/Repositories/IWatcherRepository.cs index cc82511..06db2bc 100644 --- a/VCinema/Repositories/IWatcherRepository.cs +++ b/VCinema/Repositories/IWatcherRepository.cs @@ -9,8 +9,9 @@ namespace VCinemaApi.Repositories 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); + Watcher SetWatcherNameByConnectionId(string connectionId, string name); + Watcher SetWatcherScreenByConnectionId(string connectionId, int screenId); + Watcher UnsetWatcherScreenByConnectionId(string connectionId); + void DeleteWatcherByConnectionId(string connectionId); } } diff --git a/VCinema/Repositories/MockScreenRepository.cs b/VCinema/Repositories/MockScreenRepository.cs index e3fd6f9..8c36b4b 100644 --- a/VCinema/Repositories/MockScreenRepository.cs +++ b/VCinema/Repositories/MockScreenRepository.cs @@ -30,11 +30,11 @@ namespace VCinemaApi.Repositories }; } - public Screen GetScreenById(int screenId) + public Screen GetScreenById(int id) { return new Screen { - ScreenId = 1, + ScreenId = id, Name = "Kirby's screen", Source = "https://vcinema.b-cdn.net/shrek.mp4", PlayStateUpdated = DateTime.UtcNow, @@ -43,20 +43,20 @@ namespace VCinemaApi.Repositories }; } - public Screen AddScreen(string screenName) + public Screen AddScreen(string name, string source) { return new Screen { ScreenId = 1, - Name = "Kirby's screen", - Source = "https://vcinema.b-cdn.net/shrek.mp4", + Name = name, + Source = source, PlayStateUpdated = DateTime.UtcNow, PlayState = true, PlayOffset = 1337 }; } - public void DeleteScreen(int screenId) + public void DeleteScreenById(int screenId) { } diff --git a/VCinema/Repositories/MockWatcherRepository.cs b/VCinema/Repositories/MockWatcherRepository.cs index 998ae7a..b6d01e2 100644 --- a/VCinema/Repositories/MockWatcherRepository.cs +++ b/VCinema/Repositories/MockWatcherRepository.cs @@ -60,7 +60,18 @@ namespace VCinemaApi.Repositories }; } - public Watcher AddWatcher(string connectionId, int screenId) + public Watcher SetWatcherName(string connectionId, string name) + { + return new Watcher + { + WatcherId = 1, + ConnectionId = connectionId, + Name = "Kirby", + Screen = null + }; + } + + public Watcher SetWatcherScreen(string connectionId, int screenId) { return new Watcher { @@ -79,7 +90,7 @@ namespace VCinemaApi.Repositories }; } - public Watcher SetWatcherName(string connectionId, string name) + public Watcher UnsetWatcherScreen(string connectionId) { return new Watcher { diff --git a/VCinema/Repositories/ScreenRepository.cs b/VCinema/Repositories/ScreenRepository.cs index 08e869d..d96e95d 100644 --- a/VCinema/Repositories/ScreenRepository.cs +++ b/VCinema/Repositories/ScreenRepository.cs @@ -18,16 +18,17 @@ namespace VCinemaApi.Repositories return _context.Screens.ToList(); } - public Screen GetScreenById(int screenId) + public Screen GetScreenById(int id) { - return _context.Screens.Find(screenId); + return _context.Screens.Find(id); } - public Screen AddScreen(string screenName) + public Screen AddScreen(string name, string source) { var screen = new Screen { - Name = screenName + Name = name, + Source = source }; _context.Screens.Add(screen); @@ -36,7 +37,7 @@ namespace VCinemaApi.Repositories return screen; } - public void DeleteScreen(int screenId) + public void DeleteScreenById(int screenId) { var screen = GetScreenById(screenId); _context.Screens.Remove(screen); diff --git a/VCinema/Repositories/WatcherRepository.cs b/VCinema/Repositories/WatcherRepository.cs index 88ffad8..8ae298a 100644 --- a/VCinema/Repositories/WatcherRepository.cs +++ b/VCinema/Repositories/WatcherRepository.cs @@ -36,9 +36,19 @@ namespace VCinemaApi.Repositories return watcher; } - public Watcher AddWatcher(string connectionId, int screenId) + public Watcher SetWatcherNameByConnectionId(string connectionId, string name) { - var watcher = _context.Watchers.SingleOrDefault(watcher => watcher.ConnectionId == connectionId); + var watcher = GetWatcherByConnectionId(connectionId); + + watcher.Name = name; + _context.SaveChanges(); + + return watcher; + } + + public Watcher SetWatcherScreenByConnectionId(string connectionId, int screenId) + { + var watcher = GetWatcherByConnectionId(connectionId); var screen = _context.Screens.Find(screenId); watcher.Screen = screen; @@ -47,17 +57,17 @@ namespace VCinemaApi.Repositories return watcher; } - public Watcher SetWatcherName(string connectionId, string name) + public Watcher UnsetWatcherScreenByConnectionId(string connectionId) { - var watcher = _context.Watchers.SingleOrDefault(watcher => watcher.ConnectionId == connectionId); + var watcher = GetWatcherByConnectionId(connectionId); - watcher.Name = name; + watcher.Screen = null; _context.SaveChanges(); return watcher; } - public void DeleteWatcher(string connectionId) + public void DeleteWatcherByConnectionId(string connectionId) { var watcher = GetWatcherByConnectionId(connectionId); _context.Watchers.Remove(watcher);