├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── Streamus Web API Tests ├── Controller Tests │ ├── ClientErrorControllerTest.cs │ ├── EmailControllerTest.cs │ ├── PlaylistControllerTest.cs │ ├── PlaylistItemControllerTest.cs │ └── UserControllerTest.cs ├── Helpers.cs ├── Manager Tests │ ├── EmailManagerTest.cs │ ├── PlaylistItemManagerTest.cs │ ├── PlaylistManagerTest.cs │ ├── ShareCodeManagerTest.cs │ └── UserManagerTest.cs ├── Persistance Mapping Tests │ ├── ClientErrorMappingTest.cs │ ├── PlaylistItemMappingTest.cs │ ├── PlaylistMappingTest.cs │ └── UserMappingTest.cs ├── Properties │ └── AssemblyInfo.cs ├── ResetDatabase.cs ├── Routing Tests │ ├── ClientErrorRoutingTest.cs │ ├── EmailRoutingTest.cs │ ├── PlaylistItemRoutingTest.cs │ ├── PlaylistRoutingTest.cs │ ├── ShareCodeRoutingTest.cs │ └── UserRoutingTest.cs ├── Streamus Web API Tests.csproj ├── StreamusTest.cs ├── app.config └── packages.config ├── Streamus Web API ├── App_Start │ └── WebApiConfig.cs ├── Controllers │ ├── ClientErrorController.cs │ ├── EmailController.cs │ ├── PlaylistController.cs │ ├── PlaylistItemController.cs │ ├── ShareCodeController.cs │ ├── StreamusController.cs │ ├── ThrottleAttribute.cs │ └── UserController.cs ├── Dao │ ├── AbstractNHibernateDao.cs │ ├── AutofacRegistrations.cs │ ├── ClientErrorDao.cs │ ├── Mappings │ │ ├── ClientErrorMapping.cs │ │ ├── PlaylistItemMapping.cs │ │ ├── PlaylistMapping.cs │ │ ├── ShareCodeMapping.cs │ │ └── UserMapping.cs │ ├── NHibernateConfiguration.cs │ ├── NHibernateDaoFactory.cs │ ├── PlaylistDao.cs │ ├── PlaylistItemDao.cs │ ├── ShareCodeDao.cs │ └── UserDao.cs ├── Domain │ ├── AbstractDomainEntity.cs │ ├── AbstractShareableEntity.cs │ ├── ClientError.cs │ ├── Email.cs │ ├── Interfaces │ │ ├── IAbstractDomainEntity.cs │ │ ├── IClientErrorDao.cs │ │ ├── IClientErrorManager.cs │ │ ├── IDao.cs │ │ ├── IDaoFactory.cs │ │ ├── IEmailManager.cs │ │ ├── IManagerFactory.cs │ │ ├── IPlaylistDao.cs │ │ ├── IPlaylistItemDao.cs │ │ ├── IPlaylistItemManager.cs │ │ ├── IPlaylistManager.cs │ │ ├── IShareCodeDao.cs │ │ ├── IShareCodeManager.cs │ │ ├── IShareableEntity.cs │ │ ├── IUserDao.cs │ │ └── IUserManager.cs │ ├── Managers │ │ ├── ClientErrorManager.cs │ │ ├── EmailManager.cs │ │ ├── PlaylistItemManager.cs │ │ ├── PlaylistManager.cs │ │ ├── ShareCodeManager.cs │ │ ├── StreamusManager.cs │ │ ├── StreamusManagerFactory.cs │ │ └── UserManager.cs │ ├── Playlist.cs │ ├── PlaylistItem.cs │ ├── ShareCode.cs │ ├── User.cs │ ├── Validators │ │ ├── ClientErrorValidator.cs │ │ ├── PlaylistItemValidator.cs │ │ ├── PlaylistValidator.cs │ │ ├── ShareCodeValidator.cs │ │ └── UserValidator.cs │ └── VideoType.cs ├── Dto │ ├── ClientErrorDto.cs │ ├── ContactDto.cs │ ├── CopyPlaylistRequestDto.cs │ ├── PlaylistDto.cs │ ├── PlaylistItemDto.cs │ ├── ShareCodeDto.cs │ ├── UserDto.cs │ └── VideoDto.cs ├── Global.asax ├── Global.asax.cs ├── Properties │ ├── AssemblyInfo.cs │ └── PublishProfiles │ │ └── aws-server.pubxml ├── Streamus Web API.csproj ├── Web.Debug.config ├── Web.Release.config ├── Web.config └── packages.config ├── Streamus.Tests ├── App.config ├── Controller Tests │ ├── ErrorControllerTest.cs │ ├── ErrorControllerTest.cs.orig │ ├── PlaylistControllerTest.cs │ ├── PlaylistControllerTest.cs.orig │ ├── PlaylistItemControllerTest.cs │ ├── PlaylistItemControllerTest.cs.orig │ ├── UserControllerTest.cs │ └── UserControllerTest.cs.orig ├── Helpers.cs ├── Helpers.cs.orig ├── InMemoryDatabaseTest.cs ├── Manager Tests │ ├── PlaylistItemManagerTest.cs │ ├── PlaylistItemManagerTest.cs.orig │ ├── PlaylistManagerTest.cs │ ├── PlaylistManagerTest.cs.orig │ ├── ShareCodeManagerTest.cs │ ├── UserManagerTest.cs │ ├── UserManagerTest.cs.orig │ ├── VideoManagerTest.cs │ └── VideoManagerTest.cs.orig ├── PersistanceMappingTests │ ├── ErrorMappingTest.cs │ ├── PlaylistItemMappingTest.cs │ ├── PlaylistMappingTest.cs │ ├── UserMappingTest.cs │ └── VideoMappingTest.cs ├── Properties │ └── AssemblyInfo.cs ├── ResetDatabase.cs ├── Streamus Server Tests.csproj ├── StreamusTest.cs ├── hibernate.cfg.xml ├── packages.config └── packages.config.orig ├── Streamus.sln ├── Streamus ├── App_Start │ ├── FilterConfig.cs │ ├── RouteConfig.cs │ └── WebApiConfig.cs ├── Controllers │ ├── ErrorController.cs │ ├── HomeController.cs │ ├── JsonNetResult.cs │ ├── PlaylistController.cs │ ├── PlaylistItemController.cs │ ├── ShareCodeController.cs │ ├── StreamusController.cs │ ├── ThrottleAttribute.cs │ ├── UserController.cs │ └── VideoController.cs ├── Dao │ ├── AbstractNHibernateDao.cs │ ├── AutofacRegistrations.cs │ ├── ErrorDao.cs │ ├── Mappings │ │ ├── ErrorMapping.cs │ │ ├── PlaylistItemMapping.cs │ │ ├── PlaylistMapping.cs │ │ ├── ShareCodeMapping.cs │ │ ├── UserMapping.cs │ │ └── VideoMapping.cs │ ├── NHibernateConfiguration.cs │ ├── NHibernateDaoFactory.cs │ ├── PlaylistDao.cs │ ├── PlaylistItemDao.cs │ ├── ShareCodeDao.cs │ ├── UserDao.cs │ └── VideoDao.cs ├── Domain │ ├── AbstractDomainEntity.cs │ ├── AbstractShareableEntity.cs │ ├── Error.cs │ ├── Interfaces │ │ ├── IAbstractDomainEntity.cs │ │ ├── IDao.cs │ │ ├── IDaoFactory.cs │ │ ├── IErrorDao.cs │ │ ├── IErrorManager.cs │ │ ├── IManagerFactory.cs │ │ ├── IPlaylistDao.cs │ │ ├── IPlaylistItemDao.cs │ │ ├── IPlaylistItemManager.cs │ │ ├── IPlaylistManager.cs │ │ ├── IShareCodeDao.cs │ │ ├── IShareCodeManager.cs │ │ ├── IShareableEntity.cs │ │ ├── IUserDao.cs │ │ ├── IUserManager.cs │ │ ├── IVideoDao.cs │ │ └── IVideoManager.cs │ ├── Managers │ │ ├── ErrorManager.cs │ │ ├── PlaylistItemManager.cs │ │ ├── PlaylistManager.cs │ │ ├── ShareCodeManager.cs │ │ ├── StreamusManager.cs │ │ ├── StreamusManagerFactory.cs │ │ ├── UserManager.cs │ │ └── VideoManager.cs │ ├── Playlist.cs │ ├── PlaylistItem.cs │ ├── ShareCode.cs │ ├── User.cs │ ├── Validators │ │ ├── ErrorValidator.cs │ │ ├── PlaylistItemValidator.cs │ │ ├── PlaylistValidator.cs │ │ ├── ShareCodeValidator.cs │ │ ├── UserValidator.cs │ │ └── VideoValidator.cs │ └── Video.cs ├── Dto │ ├── ErrorDto.cs │ ├── PlaylistDto.cs │ ├── PlaylistItemDto.cs │ ├── ShareCodeDto.cs │ ├── UserDto.cs │ └── VideoDto.cs ├── Global.asax ├── Global.asax.cs ├── HibernatingRhinos.Profiler.Appender.dll ├── Properties │ ├── AssemblyInfo.cs │ └── PublishProfiles │ │ └── Streamus Staging.pubxml ├── Streamus Server.csproj ├── Views │ ├── Home │ │ └── Index.cshtml │ ├── Shared │ │ ├── Error.cshtml │ │ └── _Layout.cshtml │ ├── Web.config │ └── _ViewStart.cshtml ├── Web.Debug.config ├── Web.Release.config ├── Web.config ├── hibernate.cfg.xml └── packages.config └── packages └── repositories.config /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Streamus™ 2 | ========= 3 | 4 | A Google Chrome extension which allows users to add YouTube videos to playlists, share playlists and discover new YouTube videos. 5 | 6 | Overview 7 | ======== 8 | 9 | Streamus consists of a front-end client, the Google Chrome extension, a back-end server and a website. This repository contains the files for the server. Please see the other repositories, StreamusChromeExtension and StreamusWebsite, to gain a full understanding of the product. 10 | 11 | The server's modules are managed by NuGet Package Manager. 12 | 13 | The server is used to record information about a given user's listening experience. All videos, playlist items, playlists and folders are written to the database. 14 | The server is used to enable sharing of playlists between users by copying a playlist row and providing a link to the new row to other users. 15 | 16 | 17 | Server 18 | ------ 19 | 20 | * [C# ASP.NET MVC (v4.0)](http://www.asp.net/mvc/mvc4) 21 | * [NUnit (v2.0+)](http://www.nunit.org/) 22 | * [NHibernate (v3.3.3+)](http://nhforge.org/) 23 | * [AutoFac (v3.1.1+)](https://code.google.com/p/autofac/) 24 | * [AutoMapper](https://github.com/AutoMapper/AutoMapper) 25 | * [log4net](http://logging.apache.org/log4net/) 26 | 27 | 28 | Installation 29 | ======== 30 | 31 | 1. Build 'Streamus Server' in Visual Studio 2012. 32 | 2. Build 'Streamus Server Tests' in Visual Studio 2012. 33 | 3. Create a new, local database called 'Streamus' 34 | 4. Run the test case 'ResetDatabase' to populate the database with tables and schema information. 35 | 5. Ensure all other test cases pass. 36 | 6. Run Streamus Server. 37 | 38 | Deployment 39 | ======== 40 | 41 | The server is hosted on AWS. To deploy, publish the solution to AWS via Visual Studio. Note that the web.config file will not be overidden as to not misconfigure the database connection strings. 42 | 43 | License 44 | ======= 45 | This work is licensed under the GNU General Public License v2 (GPL-2.0) 46 | 47 | Authors 48 | ======= 49 | 50 | * MeoMix - Original developer, main contributor. -------------------------------------------------------------------------------- /Streamus Web API Tests/Controller Tests/EmailControllerTest.cs: -------------------------------------------------------------------------------- 1 | using System.Net.Http; 2 | using System.Web.Http; 3 | using Moq; 4 | using NUnit.Framework; 5 | using Streamus_Web_API.Controllers; 6 | using Streamus_Web_API.Domain.Interfaces; 7 | using Streamus_Web_API.Dto; 8 | 9 | namespace Streamus_Web_API_Tests.Controller_Tests 10 | { 11 | [TestFixture] 12 | public class EmailControllerTest : StreamusTest 13 | { 14 | private EmailController _emailController; 15 | 16 | [SetUp] 17 | public new void TestFixtureSetUp() 18 | { 19 | Mock mockManagerFactory = new Mock(); 20 | Mock mockEmailManager = new Mock(); 21 | mockManagerFactory.Setup(mf => mf.GetEmailManager()).Returns(mockEmailManager.Object); 22 | _emailController = new EmailController(Logger, Session, mockManagerFactory.Object); 23 | } 24 | 25 | [Test] 26 | public void SendEmail_ValidContactForm_EmailSent() 27 | { 28 | _emailController.Request = new HttpRequestMessage(); 29 | _emailController.Request.SetConfiguration(new HttpConfiguration()); 30 | var contactFormDto = new ContactDto("John Doe", "John.Doe@gmail.com", "Hello, world!"); 31 | 32 | HttpResponseMessage response = _emailController.Create(contactFormDto); 33 | Assert.IsTrue(response.IsSuccessStatusCode); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Streamus Web API Tests/Manager Tests/EmailManagerTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Web; 3 | using NUnit.Framework; 4 | using Streamus_Web_API.Domain; 5 | using Streamus_Web_API.Domain.Interfaces; 6 | 7 | namespace Streamus_Web_API_Tests.Manager_Tests 8 | { 9 | [TestFixture] 10 | public class EmailManagerTest : StreamusTest 11 | { 12 | private IEmailManager _emailManager; 13 | 14 | /// 15 | /// This code runs before every test. 16 | /// 17 | [SetUp] 18 | public void SetupContext() 19 | { 20 | _emailManager = ManagerFactory.GetEmailManager(); 21 | HttpContext.Current = new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null)); 22 | } 23 | 24 | /// 25 | /// Send an email and ensure it was sent without exception. 26 | /// 27 | [Test] 28 | public void SendEmail_EmailIsValid_EmailSent() 29 | { 30 | bool exceptionEncountered = false; 31 | 32 | try 33 | { 34 | _emailManager.SendEmail(new Email("Hello", "World")); 35 | } 36 | catch (Exception) 37 | { 38 | exceptionEncountered = true; 39 | } 40 | 41 | Assert.IsFalse(exceptionEncountered); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Streamus Web API Tests/Manager Tests/PlaylistManagerTest.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using NUnit.Framework; 3 | using Streamus_Web_API.Domain; 4 | using Streamus_Web_API.Domain.Interfaces; 5 | 6 | namespace Streamus_Web_API_Tests.Manager_Tests 7 | { 8 | [TestFixture] 9 | public class PlaylistManagerTest : StreamusTest 10 | { 11 | private IPlaylistManager _playlistManager; 12 | 13 | private User User { get; set; } 14 | 15 | /// 16 | /// This code is only ran once for the given TestFixture. 17 | /// 18 | [SetUp] 19 | public new void TestFixtureSetUp() 20 | { 21 | _playlistManager = ManagerFactory.GetPlaylistManager(Session); 22 | 23 | User = Helpers.CreateUser(); 24 | } 25 | 26 | /// 27 | /// Verifies that a Playlist can be deleted properly. 28 | /// 29 | [Test] 30 | public void DeletePlaylist() 31 | { 32 | // Create a new Playlist and write it to the database. 33 | string title = $"New Playlist {User.Playlists.Count:D4}"; 34 | var playlist = new Playlist(title); 35 | 36 | User.AddPlaylist(playlist); 37 | 38 | _playlistManager.Save(playlist); 39 | 40 | // Now delete the created Playlist and ensure it is removed. 41 | _playlistManager.Delete(playlist.Id); 42 | 43 | 44 | bool exceptionEncountered = false; 45 | try 46 | { 47 | Playlist deletedPlaylist = _playlistManager.Get(playlist.Id); 48 | } 49 | catch (ObjectNotFoundException) 50 | { 51 | exceptionEncountered = true; 52 | } 53 | 54 | Assert.IsTrue(exceptionEncountered); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /Streamus Web API Tests/Manager Tests/ShareCodeManagerTest.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API_Tests.Manager_Tests 2 | { 3 | // TODO: Test getting a Playlist ShareCode. 4 | public class ShareCodeManagerTest : StreamusTest 5 | { 6 | } 7 | } -------------------------------------------------------------------------------- /Streamus Web API Tests/Persistance Mapping Tests/ClientErrorMappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentNHibernate.Testing; 3 | using NUnit.Framework; 4 | using Streamus_Web_API.Domain; 5 | 6 | namespace Streamus_Web_API_Tests.Persistance_Mapping_Tests 7 | { 8 | [TestFixture] 9 | public class ClientErrorMappingTest : StreamusTest 10 | { 11 | [Test] 12 | public void ShouldMap() 13 | { 14 | using (Session.BeginTransaction()) 15 | { 16 | new PersistenceSpecification(Session) 17 | .CheckProperty(e => e.LineNumber, 1) 18 | .CheckProperty(e => e.Message, "testmessage") 19 | .CheckProperty(e => e.OperatingSystem, "os") 20 | .CheckProperty(e => e.TimeOccurred, DateTime.Today) 21 | .CheckProperty(e => e.Url, "url") 22 | .CheckProperty(e => e.Architecture, "arch") 23 | .CheckProperty(e => e.ClientVersion, "0.164") 24 | .CheckProperty(e => e.UserId, Guid.NewGuid()) 25 | .CheckProperty(e => e.InstanceId, "instance_1") 26 | .CheckProperty(e => e.Stack, "stacktrace") 27 | .CheckProperty(e => e.BrowserVersion, "chrome v39") 28 | .VerifyTheMappings(); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Streamus Web API Tests/Persistance Mapping Tests/PlaylistItemMappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Streamus_Web_API.Domain; 4 | 5 | namespace Streamus_Web_API_Tests.Persistance_Mapping_Tests 6 | { 7 | [TestFixture] 8 | public class PlaylistItemMappingTest : StreamusTest 9 | { 10 | [Test] 11 | public void ShouldMap() 12 | { 13 | using (var transaction = Session.BeginTransaction()) 14 | { 15 | var createdUser = new User 16 | { 17 | GooglePlusId = "some id?" 18 | }; 19 | Session.Save(createdUser); 20 | 21 | var playlist2 = new Playlist("users second playlist") 22 | { 23 | User = createdUser, 24 | Sequence = 200, 25 | }; 26 | 27 | var playlistItem = new PlaylistItem 28 | { 29 | Playlist = playlist2, 30 | VideoId = "some id", 31 | Author = "author", 32 | Duration = 90, 33 | Sequence = 300, 34 | VideoTitle = "some video title", 35 | }; 36 | 37 | playlist2.AddItem(playlistItem); 38 | 39 | Session.Save(playlist2); 40 | 41 | Session.Flush(); 42 | Session.Clear(); 43 | 44 | var savedPlaylistItem = Session.Get(playlistItem.Id); 45 | 46 | Assert.That(savedPlaylistItem.VideoTitle, Is.EqualTo("some video title")); 47 | Assert.That(savedPlaylistItem.Id, Is.Not.EqualTo(Guid.Empty)); 48 | Assert.That(savedPlaylistItem.Sequence, Is.EqualTo(300)); 49 | 50 | transaction.Rollback(); 51 | } 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /Streamus Web API Tests/Persistance Mapping Tests/PlaylistMappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Streamus_Web_API.Domain; 4 | 5 | namespace Streamus_Web_API_Tests.Persistance_Mapping_Tests 6 | { 7 | [TestFixture] 8 | public class PlaylistMappingTest : StreamusTest 9 | { 10 | [Test] 11 | public void ShouldMap() 12 | { 13 | using (var transaction = Session.BeginTransaction()) 14 | { 15 | var createdUser = new User 16 | { 17 | GooglePlusId = "some id?" 18 | }; 19 | 20 | Session.Save(createdUser); 21 | 22 | var playlist2 = new Playlist("users second playlist") 23 | { 24 | User = createdUser, 25 | Sequence = 200, 26 | }; 27 | 28 | var playlistItem = new PlaylistItem 29 | { 30 | Playlist = playlist2, 31 | Sequence = 200, 32 | }; 33 | 34 | playlist2.AddItem(playlistItem); 35 | 36 | var playlistId = Session.Save(playlist2); 37 | 38 | Session.Flush(); 39 | Session.Clear(); 40 | 41 | var savedPlaylist = Session.Get(playlistId); 42 | 43 | Assert.That(savedPlaylist.Title, Is.EqualTo("users second playlist")); 44 | Assert.That(savedPlaylist.Id, Is.Not.EqualTo(Guid.Empty)); 45 | Assert.That(savedPlaylist.Sequence, Is.EqualTo(200)); 46 | 47 | Assert.That(savedPlaylist.Items, Has.Exactly(1).EqualTo(playlistItem)); 48 | 49 | transaction.Rollback(); 50 | } 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /Streamus Web API Tests/Persistance Mapping Tests/UserMappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using NHibernate.Linq; 4 | using NUnit.Framework; 5 | using Streamus_Web_API.Domain; 6 | 7 | namespace Streamus_Web_API_Tests.Persistance_Mapping_Tests 8 | { 9 | [TestFixture] 10 | public class UserMappingTest : StreamusTest 11 | { 12 | [Test] 13 | public void ShouldMap() 14 | { 15 | using (var transaction = Session.BeginTransaction()) 16 | { 17 | var createdUser = new User 18 | { 19 | GooglePlusId = "some id?" 20 | }; 21 | 22 | var playlist = new Playlist("boss videos") 23 | { 24 | User = createdUser 25 | }; 26 | 27 | createdUser.AddPlaylist(playlist); 28 | 29 | Session.Save(createdUser); 30 | Session.Flush(); 31 | Session.Clear(); 32 | 33 | var savedUser = Session.Get(createdUser.Id); 34 | Assert.That(savedUser.Id, Is.Not.EqualTo(Guid.Empty)); 35 | 36 | Assert.That(savedUser.GooglePlusId, Is.EqualTo(createdUser.GooglePlusId)); 37 | Assert.That(savedUser.Playlists, Has.Count.EqualTo(2)); 38 | 39 | Assert.That(Session.Query().Where(p => p.User == savedUser).ToList(), Has.Count.EqualTo(2)); 40 | 41 | transaction.Rollback(); 42 | } 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /Streamus Web API Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Streamus Web API Tests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Streamus Web API Tests")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("dd016178-9b14-41f8-9075-1417e29dbd0d")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Streamus Web API Tests/ResetDatabase.cs: -------------------------------------------------------------------------------- 1 | using NHibernate.Tool.hbm2ddl; 2 | using NUnit.Framework; 3 | using Streamus_Web_API.Dao; 4 | 5 | namespace Streamus_Web_API_Tests 6 | { 7 | /// 8 | /// This isn't a test, but is grouped with testing because of how useful it is. 9 | /// Running this 'test' will analyze the hbm.xml NHibernate files, determine a database schema 10 | /// derived from those files and reset the test database such that it reflects the current NHibernate schema. 11 | /// You can then use a DbDiff tool to propagate the changes to a production database. 12 | /// Inconclusive means it was ran successfully. If it errors out, try running again. 13 | /// 14 | [TestFixture] 15 | public class ResetDatabase 16 | { 17 | /// 18 | /// This code is only ran once for the given TestFixture. 19 | /// 20 | [TestFixtureSetUp] 21 | public void TestFixtureSetUp() 22 | { 23 | new SchemaExport((new NHibernateConfiguration()).Configure().BuildConfiguration()).Execute(false, true, false); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Streamus Web API Tests/Routing Tests/ClientErrorRoutingTest.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | using Streamus_Web_API.Controllers; 4 | using System.Net.Http; 5 | using System.Web.Http; 6 | using Streamus_Web_API; 7 | 8 | namespace Streamus_Web_API_Tests.Routing_Tests 9 | { 10 | [TestFixture] 11 | public class ClientErrorRoutingTest : StreamusTest 12 | { 13 | private const string RoutePrefix = "http://localhost/ClientError/"; 14 | 15 | [Test] 16 | public void POST_error_Should_route_to_ErrorController_Create_method() 17 | { 18 | var request = new HttpRequestMessage(HttpMethod.Post, RoutePrefix); 19 | var config = new HttpConfiguration(); 20 | 21 | WebApiConfig.Register(config); 22 | var route = Helpers.RouteRequest(config, request); 23 | 24 | route.Controller.Should().Be(); 25 | route.Action.Should().Be("Create"); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Streamus Web API Tests/Routing Tests/EmailRoutingTest.cs: -------------------------------------------------------------------------------- 1 | using System.Net.Http; 2 | using System.Web.Http; 3 | using FluentAssertions; 4 | using NUnit.Framework; 5 | using Streamus_Web_API; 6 | using Streamus_Web_API.Controllers; 7 | 8 | namespace Streamus_Web_API_Tests.Routing_Tests 9 | { 10 | [TestFixture] 11 | public class EmailRoutingTest : StreamusTest 12 | { 13 | private const string RoutePrefix = "http://localhost/Email/"; 14 | 15 | [Test] 16 | public void Post_ContactFormDto_RouteToEmailControllerCreate() 17 | { 18 | var request = new HttpRequestMessage(HttpMethod.Post, RoutePrefix); 19 | var config = new HttpConfiguration(); 20 | 21 | WebApiConfig.Register(config); 22 | var route = Helpers.RouteRequest(config, request); 23 | 24 | route.Controller.Should().Be(); 25 | route.Action.Should().Be("Create"); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Streamus Web API Tests/Routing Tests/PlaylistItemRoutingTest.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | using Streamus_Web_API.Controllers; 4 | using System; 5 | using System.Net.Http; 6 | using System.Web.Http; 7 | using Streamus_Web_API; 8 | 9 | namespace Streamus_Web_API_Tests.Routing_Tests 10 | { 11 | [TestFixture] 12 | public class PlaylistItemRoutingTest : StreamusTest 13 | { 14 | private const string RoutePrefix = "http://localhost/PlaylistItem/"; 15 | 16 | [Test] 17 | public void POST_Should_route_to_PlaylistItemController_Create_method() 18 | { 19 | var request = new HttpRequestMessage(HttpMethod.Post, RoutePrefix); 20 | var config = new HttpConfiguration(); 21 | 22 | WebApiConfig.Register(config); 23 | var route = Helpers.RouteRequest(config, request); 24 | 25 | route.Controller.Should().Be(); 26 | route.Action.Should().Be("Create"); 27 | } 28 | 29 | [Test] 30 | public void POST_CreateMultiple_Should_route_to_PlaylistItemController_CreateMultiple_method() 31 | { 32 | var request = new HttpRequestMessage(HttpMethod.Post, RoutePrefix + "CreateMultiple/"); 33 | var config = new HttpConfiguration(); 34 | 35 | WebApiConfig.Register(config); 36 | var route = Helpers.RouteRequest(config, request); 37 | 38 | route.Controller.Should().Be(); 39 | route.Action.Should().Be("CreateMultiple"); 40 | } 41 | 42 | [Test] 43 | public void PATCH_Should_route_to_PlaylistItemController_Patch_method() 44 | { 45 | var request = new HttpRequestMessage(new HttpMethod("PATCH"), RoutePrefix + Guid.NewGuid()); 46 | var config = new HttpConfiguration(); 47 | 48 | WebApiConfig.Register(config); 49 | var route = Helpers.RouteRequest(config, request); 50 | 51 | route.Controller.Should().Be(); 52 | route.Action.Should().Be("Patch"); 53 | } 54 | 55 | [Test] 56 | public void DELETE_Should_route_to_PlaylistItemController_Delete_method() 57 | { 58 | var request = new HttpRequestMessage(HttpMethod.Delete, RoutePrefix + Guid.NewGuid()); 59 | var config = new HttpConfiguration(); 60 | 61 | WebApiConfig.Register(config); 62 | var route = Helpers.RouteRequest(config, request); 63 | 64 | route.Controller.Should().Be(); 65 | route.Action.Should().Be("Delete"); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Streamus Web API Tests/Routing Tests/PlaylistRoutingTest.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | using Streamus_Web_API.Controllers; 4 | using System; 5 | using System.Net.Http; 6 | using System.Web.Http; 7 | using Streamus_Web_API; 8 | 9 | namespace Streamus_Web_API_Tests.Routing_Tests 10 | { 11 | [TestFixture] 12 | public class PlaylistRoutingTest : StreamusTest 13 | { 14 | private const string RoutePrefix = "http://localhost/Playlist/"; 15 | 16 | [Test] 17 | public void POST_Should_route_to_PlaylistController_Create_method() 18 | { 19 | var request = new HttpRequestMessage(HttpMethod.Post, RoutePrefix); 20 | var config = new HttpConfiguration(); 21 | 22 | WebApiConfig.Register(config); 23 | var route = Helpers.RouteRequest(config, request); 24 | 25 | route.Controller.Should().Be(); 26 | route.Action.Should().Be("Create"); 27 | } 28 | 29 | [Test] 30 | public void GET_Should_route_to_PlaylistController_Get_method() 31 | { 32 | var request = new HttpRequestMessage(HttpMethod.Get, RoutePrefix + Guid.NewGuid()); 33 | var config = new HttpConfiguration(); 34 | 35 | WebApiConfig.Register(config); 36 | var route = Helpers.RouteRequest(config, request); 37 | 38 | route.Controller.Should().Be(); 39 | route.Action.Should().Be("Get"); 40 | } 41 | 42 | [Test] 43 | public void DELETE_Should_route_to_PlaylistController_Delete_method() 44 | { 45 | var request = new HttpRequestMessage(HttpMethod.Delete, RoutePrefix + Guid.NewGuid()); 46 | var config = new HttpConfiguration(); 47 | 48 | WebApiConfig.Register(config); 49 | var route = Helpers.RouteRequest(config, request); 50 | 51 | route.Controller.Should().Be(); 52 | route.Action.Should().Be("Delete"); 53 | } 54 | 55 | [Test] 56 | public void PATCH_Should_route_to_PlaylistController_Patch_method() 57 | { 58 | var request = new HttpRequestMessage(new HttpMethod("PATCH"), RoutePrefix + Guid.NewGuid()); 59 | var config = new HttpConfiguration(); 60 | 61 | WebApiConfig.Register(config); 62 | var route = Helpers.RouteRequest(config, request); 63 | 64 | route.Controller.Should().Be(); 65 | route.Action.Should().Be("Patch"); 66 | } 67 | 68 | [Test] 69 | public void POST_CreateCopyByShareCode_Should_route_to_PlaylistController_CreateCopyByShareCode_method() 70 | { 71 | var request = new HttpRequestMessage(HttpMethod.Post, RoutePrefix + "Copy/"); 72 | var config = new HttpConfiguration(); 73 | 74 | WebApiConfig.Register(config); 75 | var route = Helpers.RouteRequest(config, request); 76 | 77 | route.Controller.Should().Be(); 78 | route.Action.Should().Be("Copy"); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Streamus Web API Tests/Routing Tests/ShareCodeRoutingTest.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | using Streamus_Web_API.Controllers; 4 | using System; 5 | using System.Net.Http; 6 | using System.Web.Http; 7 | using Streamus_Web_API; 8 | 9 | namespace Streamus_Web_API_Tests.Routing_Tests 10 | { 11 | [TestFixture] 12 | class ShareCodeRoutingTest : StreamusTest 13 | { 14 | private const string RoutePrefix = "http://localhost/ShareCode/"; 15 | 16 | [Test] 17 | public void GET_GetShareCode_Should_route_to_ShareCodeController_GetShareCode_method() 18 | { 19 | var request = new HttpRequestMessage(HttpMethod.Get, RoutePrefix + "GetShareCode/?playlistId=" + Guid.NewGuid()); 20 | var config = new HttpConfiguration(); 21 | 22 | WebApiConfig.Register(config); 23 | var route = Helpers.RouteRequest(config, request); 24 | 25 | route.Controller.Should().Be(); 26 | route.Action.Should().Be("GetShareCode"); 27 | } 28 | 29 | [Test] 30 | public void GET_ShareCodeByShortIdAndEntityTitle_Should_route_to_ShareCodeController_GetShareCodeByShortIdAndEntityTitle_method() 31 | { 32 | var request = new HttpRequestMessage(HttpMethod.Get, RoutePrefix + "abc123/foo"); 33 | var config = new HttpConfiguration(); 34 | 35 | WebApiConfig.Register(config); 36 | var route = Helpers.RouteRequest(config, request); 37 | 38 | route.Controller.Should().Be(); 39 | route.Action.Should().Be("GetShareCodeByShortIdAndEntityTitle"); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Streamus Web API Tests/StreamusTest.cs: -------------------------------------------------------------------------------- 1 | using System.Net.Http; 2 | using Streamus_Web_API.Dao; 3 | using log4net; 4 | using NHibernate; 5 | using NUnit.Framework; 6 | using Streamus_Web_API; 7 | using Streamus_Web_API.Domain.Interfaces; 8 | using System.Web.Http; 9 | using System.Web.Http.Dependencies; 10 | 11 | namespace Streamus_Web_API_Tests 12 | { 13 | public abstract class StreamusTest 14 | { 15 | protected ILog Logger; 16 | protected IDaoFactory DaoFactory; 17 | protected IManagerFactory ManagerFactory; 18 | protected Helpers Helpers; 19 | protected ISession Session; 20 | 21 | private HttpRequestMessage _httpRequestMessage; 22 | 23 | [TestFixtureSetUp] 24 | public void TestFixtureSetUp() 25 | { 26 | StreamusWebApi.InitializeApplication(); 27 | } 28 | 29 | [SetUp] 30 | public void SetUp() 31 | { 32 | HttpConfiguration httpConfiguration = new HttpConfiguration(); 33 | AutofacRegistrations.RegisterAndSetResolver(httpConfiguration); 34 | 35 | _httpRequestMessage = new HttpRequestMessage(); 36 | _httpRequestMessage.SetConfiguration(httpConfiguration); 37 | 38 | IDependencyScope dependencyScope = _httpRequestMessage.GetDependencyScope(); 39 | 40 | Logger = (ILog)dependencyScope.GetService(typeof(ILog)); 41 | DaoFactory = (IDaoFactory)dependencyScope.GetService(typeof(IDaoFactory)); 42 | Session = (ISession)dependencyScope.GetService(typeof(ISession)); 43 | ManagerFactory = (IManagerFactory)dependencyScope.GetService(typeof(IManagerFactory)); 44 | 45 | Helpers = new Helpers(Session, ManagerFactory); 46 | } 47 | 48 | [TearDown] 49 | public void TearDown() 50 | { 51 | _httpRequestMessage.Dispose(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Streamus Web API Tests/app.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Streamus Web API Tests/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Streamus Web API/App_Start/WebApiConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Http; 2 | using System.Web.Http.Cors; 3 | using Streamus_Web_API.Dao; 4 | 5 | namespace Streamus_Web_API 6 | { 7 | public static class WebApiConfig 8 | { 9 | public static void Register(HttpConfiguration config) 10 | { 11 | var cors = new EnableCorsAttribute("http://dev.streamus.com:8080,https://streamus.com,chrome-extension://jbnkffmindojffecdhbbmekbmkkfpmjd,chrome-extension://nnmcpagedcgekmljdamaeahfbmmjloho", "*", "*"); 12 | config.EnableCors(cors); 13 | 14 | AutofacRegistrations.RegisterAndSetResolver(config); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Streamus Web API/Controllers/ClientErrorController.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Http; 2 | using Streamus_Web_API.Domain; 3 | using Streamus_Web_API.Domain.Interfaces; 4 | using Streamus_Web_API.Dto; 5 | using log4net; 6 | using NHibernate; 7 | 8 | namespace Streamus_Web_API.Controllers 9 | { 10 | [RoutePrefix("ClientError")] 11 | public class ClientErrorController : StreamusController 12 | { 13 | private readonly IClientErrorManager _clientErrorManager; 14 | 15 | public ClientErrorController(ILog logger, ISession session, IManagerFactory managerFactory) 16 | : base(logger, session) 17 | { 18 | _clientErrorManager = managerFactory.GetErrorManager(session); 19 | } 20 | 21 | [Route("")] 22 | [HttpPost, Throttle(Name = "ClientErrorThrottle", Message = "You must wait {n} seconds before logging another client error.", Seconds = 60)] 23 | public ClientErrorDto Create(ClientErrorDto clientErrorDto) 24 | { 25 | ClientErrorDto savedErrorDto; 26 | 27 | using (ITransaction transaction = Session.BeginTransaction()) 28 | { 29 | ClientError clientError = new ClientError(clientErrorDto.InstanceId, clientErrorDto.Architecture, clientErrorDto.ClientVersion, clientErrorDto.LineNumber, clientErrorDto.BrowserVersion, clientErrorDto.Message, clientErrorDto.OperatingSystem, clientErrorDto.Url, clientErrorDto.Stack, clientErrorDto.UserId); 30 | _clientErrorManager.Save(clientError); 31 | 32 | savedErrorDto = ClientErrorDto.Create(clientError); 33 | 34 | transaction.Commit(); 35 | } 36 | 37 | return savedErrorDto; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Streamus Web API/Controllers/EmailController.cs: -------------------------------------------------------------------------------- 1 | using System.Net; 2 | using System.Net.Http; 3 | using System.Web.Http; 4 | using Streamus_Web_API.Dto; 5 | using log4net; 6 | using NHibernate; 7 | using Streamus_Web_API.Domain; 8 | using Streamus_Web_API.Domain.Interfaces; 9 | 10 | namespace Streamus_Web_API.Controllers 11 | { 12 | [RoutePrefix("Email")] 13 | public class EmailController : StreamusController 14 | { 15 | private readonly IEmailManager _emailManager; 16 | 17 | public EmailController(ILog logger, ISession session, IManagerFactory managerFactory) 18 | : base(logger, session) 19 | { 20 | _emailManager = managerFactory.GetEmailManager(); 21 | } 22 | 23 | [Route("")] 24 | [HttpPost, Throttle(Name = "EmailThrottle", Message = "Wait {n} seconds before sending another message.", Seconds = 60)] 25 | public HttpResponseMessage Create(ContactDto contactDto) 26 | { 27 | Email email = new Email($"Contact from: {contactDto.Name}", $"Email: {contactDto.Email} \n Message: {contactDto.Message}"); 28 | _emailManager.SendEmail(email); 29 | 30 | return Request.CreateResponse(HttpStatusCode.NoContent); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Streamus Web API/Controllers/StreamusController.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Http; 2 | using NHibernate; 3 | using log4net; 4 | using System; 5 | 6 | namespace Streamus_Web_API.Controllers 7 | { 8 | public abstract class StreamusController : ApiController 9 | { 10 | protected readonly ILog Logger; 11 | protected readonly ISession Session; 12 | 13 | protected StreamusController(ILog logger, ISession session) 14 | { 15 | if (logger == null) throw new ArgumentNullException(nameof(logger)); 16 | if (session == null) throw new ArgumentNullException(nameof(session)); 17 | 18 | Logger = logger; 19 | Session = session; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Streamus Web API/Dao/AbstractNHibernateDao.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using Streamus_Web_API.Domain.Interfaces; 3 | 4 | namespace Streamus_Web_API.Dao 5 | { 6 | public class AbstractNHibernateDao : IDao where T : class 7 | { 8 | protected ISession Session { get; set; } 9 | 10 | public AbstractNHibernateDao(ISession session) 11 | { 12 | Session = session; 13 | } 14 | 15 | public T Merge(T entity) 16 | { 17 | return Session.Merge(entity); 18 | } 19 | 20 | public void Save(T entity) 21 | { 22 | Session.Save(entity); 23 | } 24 | 25 | public void Update(T entity) 26 | { 27 | Session.Update(entity); 28 | } 29 | 30 | public void Delete(T entity) 31 | { 32 | Session.Delete(entity); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Streamus Web API/Dao/AutofacRegistrations.cs: -------------------------------------------------------------------------------- 1 | using Autofac; 2 | using Autofac.Integration.WebApi; 3 | using log4net; 4 | using NHibernate; 5 | using Streamus_Web_API.Domain.Interfaces; 6 | using Streamus_Web_API.Domain.Managers; 7 | using System.Reflection; 8 | using System.Web.Http; 9 | 10 | namespace Streamus_Web_API.Dao 11 | { 12 | public class AutofacRegistrations 13 | { 14 | public static void RegisterAndSetResolver(HttpConfiguration httpConfiguration) 15 | { 16 | httpConfiguration.MapHttpAttributeRoutes(); 17 | 18 | var containerBuilder = new ContainerBuilder(); 19 | 20 | containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly()); 21 | 22 | containerBuilder.Register(x => new NHibernateConfiguration().Configure().BuildSessionFactory()).SingleInstance(); 23 | 24 | containerBuilder.RegisterType().As().InstancePerRequest(); 25 | containerBuilder.RegisterType().As().InstancePerRequest(); 26 | containerBuilder.Register(x => x.Resolve().OpenSession()).InstancePerRequest(); 27 | containerBuilder.Register(x => LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType)).InstancePerRequest(); 28 | 29 | ILifetimeScope container = containerBuilder.Build(); 30 | 31 | httpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container); 32 | 33 | httpConfiguration.EnsureInitialized(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/ClientErrorDao.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using Streamus_Web_API.Domain; 3 | using Streamus_Web_API.Domain.Interfaces; 4 | 5 | namespace Streamus_Web_API.Dao 6 | { 7 | public class ClientErrorDao : AbstractNHibernateDao, IClientErrorDao 8 | { 9 | public ClientErrorDao(ISession session) 10 | : base(session) 11 | { 12 | 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/Mappings/ClientErrorMapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentNHibernate.Mapping; 3 | using Streamus_Web_API.Domain; 4 | 5 | namespace Streamus_Web_API.Dao.Mappings 6 | { 7 | public class ClientErrorMapping : ClassMap 8 | { 9 | public ClientErrorMapping() 10 | { 11 | Table("[Errors]"); 12 | 13 | Id(e => e.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty); 14 | 15 | // Only update properties which have changed. 16 | DynamicUpdate(); 17 | 18 | Map(e => e.Message).Not.Nullable().Length(ClientError.MaxMessageLength); 19 | Map(e => e.LineNumber).Not.Nullable(); 20 | Map(e => e.Url).Not.Nullable().Length(ClientError.MaxUrlLength); 21 | Map(e => e.ClientVersion).Not.Nullable().Length(ClientError.MaxClientVersionLength); 22 | Map(e => e.TimeOccurred).Not.Nullable(); 23 | Map(e => e.OperatingSystem).Not.Nullable().Length(ClientError.MaxOperatingSystemLength); 24 | Map(e => e.Architecture).Not.Nullable().Length(ClientError.MaxArchitectureLength); 25 | Map(e => e.Stack).Not.Nullable().Length(ClientError.MaxStackLength); 26 | Map(e => e.BrowserVersion).Not.Nullable().Length(ClientError.MaxBrowserVersionLength); 27 | Map(e => e.InstanceId).Not.Nullable().Length(ClientError.MaxInstanceIdLength); 28 | Map(e => e.UserId).Not.Nullable(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/Mappings/PlaylistItemMapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentNHibernate.Mapping; 3 | using Streamus_Web_API.Domain; 4 | 5 | namespace Streamus_Web_API.Dao.Mappings 6 | { 7 | public class PlaylistItemMapping : ClassMap 8 | { 9 | public PlaylistItemMapping() 10 | { 11 | Table("[PlaylistItems]"); 12 | 13 | Id(e => e.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty); 14 | 15 | // Only update properties which have changed. 16 | DynamicUpdate(); 17 | 18 | Map(e => e.Sequence).Not.Nullable(); 19 | Map(e => e.Author).Not.Nullable().Length(PlaylistItem.MaxAuthorLength); 20 | Map(e => e.Duration).Not.Nullable(); 21 | Map(e => e.VideoId).Not.Nullable().Length(PlaylistItem.MaxVideoIdLength); 22 | Map(e => e.VideoType).Not.Nullable().Length(PlaylistItem.MaxVideoTypeLength); 23 | Map(e => e.VideoTitle).Not.Nullable().Length(PlaylistItem.MaxVideoTitleLength); 24 | 25 | References(p => p.Playlist).Column("PlaylistId"); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/Mappings/PlaylistMapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentNHibernate.Mapping; 3 | using Streamus_Web_API.Domain; 4 | 5 | namespace Streamus_Web_API.Dao.Mappings 6 | { 7 | public class PlaylistMapping : ClassMap 8 | { 9 | public PlaylistMapping() 10 | { 11 | Table("[Playlists]"); 12 | Id(e => e.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty); 13 | 14 | Map(e => e.Title).Not.Nullable().Length(Playlist.MaxTitleLength); 15 | Map(e => e.Sequence).Not.Nullable(); 16 | 17 | // Only update properties which have changed. 18 | DynamicUpdate(); 19 | 20 | HasMany(p => p.Items) 21 | .Inverse() 22 | .Cascade.AllDeleteOrphan().KeyColumn("PlaylistId"); 23 | 24 | References(p => p.User).Column("UserId"); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/Mappings/ShareCodeMapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentNHibernate.Mapping; 3 | using Streamus_Web_API.Domain; 4 | 5 | namespace Streamus_Web_API.Dao.Mappings 6 | { 7 | public class ShareCodeMapping : ClassMap 8 | { 9 | public ShareCodeMapping() 10 | { 11 | Table("[ShareCodes]"); 12 | 13 | Id(e => e.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty); 14 | 15 | // Only update properties which have changed. 16 | DynamicUpdate(); 17 | 18 | Map(e => e.EntityType).Not.Nullable().Length(AbstractShareableDomainEntity.MaxEntityTypeLength); 19 | Map(e => e.EntityId).Not.Nullable(); 20 | Map(e => e.ShortId).Not.Nullable().Length(AbstractShareableDomainEntity.MaxShortIdLength); 21 | Map(e => e.UrlFriendlyEntityTitle).Not.Nullable().Length(AbstractShareableDomainEntity.MaxUrlFriendlyTitleLength); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/Mappings/UserMapping.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Mapping; 2 | using Streamus_Web_API.Domain; 3 | using System; 4 | 5 | namespace Streamus_Web_API.Dao.Mappings 6 | { 7 | public class UserMapping : ClassMap 8 | { 9 | public UserMapping() 10 | { 11 | Table("[Users]"); 12 | 13 | Not.LazyLoad(); 14 | 15 | Id(e => e.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty); 16 | 17 | // Only update properties which have changed. 18 | DynamicUpdate(); 19 | 20 | Map(e => e.GooglePlusId).Not.Nullable().Length(User.MaxGooglePlusIdLength); 21 | Map(e => e.Language).Not.Nullable().Length(User.MaxLanguageLength); 22 | 23 | HasMany(u => u.Playlists) 24 | .Inverse() 25 | // 100% of the time a user is loaded their playlists are sent back to the server, so it's OK to do this. 26 | .Not.LazyLoad() 27 | .Cascade.AllDeleteOrphan() 28 | .KeyColumn("UserId"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/NHibernateConfiguration.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentNHibernate.Cfg; 3 | using FluentNHibernate.Cfg.Db; 4 | using System.Configuration; 5 | using Streamus_Web_API.Dao.Mappings; 6 | using Configuration = NHibernate.Cfg.Configuration; 7 | using Environment = NHibernate.Cfg.Environment; 8 | 9 | namespace Streamus_Web_API.Dao 10 | { 11 | public class NHibernateConfiguration 12 | { 13 | public FluentConfiguration Configure() 14 | { 15 | string connectionString = ConfigurationManager.ConnectionStrings["default"].ConnectionString; 16 | 17 | FluentConfiguration fluentConfiguration = Fluently.Configure() 18 | .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString).ShowSql().FormatSql()) 19 | .Mappings(cfg => cfg.FluentMappings.AddFromAssemblyOf()) 20 | .ExposeConfiguration(ConfigureStreamusDataAccess); 21 | 22 | return fluentConfiguration; 23 | } 24 | 25 | private static void ConfigureStreamusDataAccess(Configuration configuration) 26 | { 27 | // NHibernate.Context.WebSessionContext - analogous to ManagedWebSessionContext above, stores the current session in HttpContext. 28 | // You are responsible to bind and unbind an ISession instance with static methods of class CurrentSessionContext. 29 | configuration.SetProperty("current_session_context_class", "web"); 30 | configuration.SetProperty("connection.isolation", "ReadUncommitted"); 31 | configuration.SetProperty(Environment.CommandTimeout, TimeSpan.FromSeconds(15).TotalSeconds.ToString()); 32 | #if DEBUG 33 | configuration.SetProperty("default_schema", "[Streamus].[dbo]"); 34 | configuration.SetProperty("generate_statistics", "true"); 35 | #else 36 | configuration.SetProperty("default_schema", "[dbo]"); 37 | #endif 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/NHibernateDaoFactory.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using Streamus_Web_API.Domain.Interfaces; 3 | 4 | namespace Streamus_Web_API.Dao 5 | { 6 | /// 7 | /// Exposes access to NHibernate DAO classes. Motivation for this DAO 8 | /// framework can be found at http://www.hibernate.org/328.html. 9 | /// 10 | public class NHibernateDaoFactory : IDaoFactory 11 | { 12 | private IClientErrorDao _clientErrorDao; 13 | private IPlaylistDao _playlistDao; 14 | private IPlaylistItemDao _playlistItemDao; 15 | private IShareCodeDao _shareCodeDao; 16 | private IUserDao _userDao; 17 | 18 | public IClientErrorDao GetErrorDao(ISession session) 19 | { 20 | return _clientErrorDao ?? (_clientErrorDao = new ClientErrorDao(session)); 21 | } 22 | 23 | public IPlaylistDao GetPlaylistDao(ISession session) 24 | { 25 | return _playlistDao ?? (_playlistDao = new PlaylistDao(session)); 26 | } 27 | 28 | public IPlaylistItemDao GetPlaylistItemDao(ISession session) 29 | { 30 | return _playlistItemDao ?? (_playlistItemDao = new PlaylistItemDao(session)); 31 | } 32 | 33 | public IShareCodeDao GetShareCodeDao(ISession session) 34 | { 35 | return _shareCodeDao ?? (_shareCodeDao = new ShareCodeDao(session)); 36 | } 37 | 38 | public IUserDao GetUserDao(ISession session) 39 | { 40 | return _userDao ?? (_userDao = new UserDao(session)); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Streamus Web API/Dao/PlaylistDao.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NHibernate; 3 | using Streamus_Web_API.Domain; 4 | using Streamus_Web_API.Domain.Interfaces; 5 | 6 | namespace Streamus_Web_API.Dao 7 | { 8 | public class PlaylistDao : AbstractNHibernateDao, IPlaylistDao 9 | { 10 | public PlaylistDao(ISession session) 11 | : base(session) 12 | { 13 | 14 | } 15 | 16 | public Playlist Get(Guid id) 17 | { 18 | Playlist playlist = null; 19 | 20 | if (id != default(Guid)) 21 | { 22 | playlist = Session.Load(id); 23 | } 24 | 25 | return playlist; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/PlaylistItemDao.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using System; 3 | using Streamus_Web_API.Domain; 4 | using Streamus_Web_API.Domain.Interfaces; 5 | 6 | namespace Streamus_Web_API.Dao 7 | { 8 | public class PlaylistItemDao : AbstractNHibernateDao, IPlaylistItemDao 9 | { 10 | public PlaylistItemDao(ISession session) 11 | : base(session) 12 | { 13 | 14 | } 15 | 16 | public PlaylistItem Get(Guid id) 17 | { 18 | PlaylistItem playlistItem = null; 19 | 20 | if (id != default(Guid)) 21 | { 22 | playlistItem = Session.Load(id); 23 | } 24 | 25 | return playlistItem; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/ShareCodeDao.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using NHibernate.Criterion; 3 | using Streamus_Web_API.Domain; 4 | using Streamus_Web_API.Domain.Interfaces; 5 | 6 | namespace Streamus_Web_API.Dao 7 | { 8 | public class ShareCodeDao : AbstractNHibernateDao, IShareCodeDao 9 | { 10 | public ShareCodeDao(ISession session) 11 | : base(session) 12 | { 13 | 14 | } 15 | 16 | public ShareCode GetByShortIdAndEntityTitle(string shareCodeShortId, string urlFriendlyEntityTitle) 17 | { 18 | ICriteria criteria = Session 19 | .CreateCriteria(typeof(ShareCode), "ShareCode") 20 | .Add(Restrictions.Eq("ShareCode.ShortId", shareCodeShortId)) 21 | .Add(Restrictions.Eq("ShareCode.UrlFriendlyEntityTitle", urlFriendlyEntityTitle)); 22 | 23 | var shareCode = criteria.UniqueResult(); 24 | 25 | return shareCode; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Streamus Web API/Dao/UserDao.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using NHibernate; 3 | using NHibernate.Criterion; 4 | using System; 5 | using Streamus_Web_API.Domain; 6 | using Streamus_Web_API.Domain.Interfaces; 7 | 8 | namespace Streamus_Web_API.Dao 9 | { 10 | public class UserDao : AbstractNHibernateDao, IUserDao 11 | { 12 | public UserDao(ISession session) 13 | : base(session) 14 | { 15 | 16 | } 17 | 18 | public User Get(Guid id) 19 | { 20 | User user = null; 21 | 22 | if (id != default(Guid)) 23 | { 24 | user = Session.Get(id); 25 | } 26 | 27 | return user; 28 | } 29 | 30 | public User GetByGooglePlusId(string googlePlusId) 31 | { 32 | User user = null; 33 | 34 | if (googlePlusId != string.Empty) 35 | { 36 | ICriteria criteria = Session 37 | .CreateCriteria(typeof(User), "User") 38 | .Add(Restrictions.Eq("User.GooglePlusId", googlePlusId)); 39 | 40 | user = criteria.UniqueResult(); 41 | } 42 | 43 | return user; 44 | } 45 | 46 | public IList GetAllByGooglePlusId(string googlePlusId) 47 | { 48 | IList users = new List(); 49 | 50 | if (googlePlusId != string.Empty) 51 | { 52 | ICriteria criteria = Session 53 | .CreateCriteria(typeof(User), "User") 54 | .Add(Restrictions.Eq("User.GooglePlusId", googlePlusId)); 55 | 56 | users = criteria.List(); 57 | } 58 | 59 | return users; 60 | } 61 | 62 | // http://stackoverflow.com/questions/3390561/nhibernate-update-single-field-without-loading-entity 63 | public void UpdateGooglePlusIds(IList ids, string googlePlusId) 64 | { 65 | Session.CreateQuery("update User set GooglePlusId = :googlePlusId where id in (:ids)") 66 | .SetParameter("googlePlusId", googlePlusId) 67 | .SetParameterList("ids", ids) 68 | .ExecuteUpdate(); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/AbstractDomainEntity.cs: -------------------------------------------------------------------------------- 1 | using Streamus_Web_API.Domain.Interfaces; 2 | 3 | namespace Streamus_Web_API.Domain 4 | { 5 | public abstract class AbstractDomainEntity : IAbstractDomainEntity 6 | { 7 | public virtual T Id { get; set; } 8 | 9 | private int? OldHashCode; 10 | public override int GetHashCode() 11 | { 12 | // Once we have a hash code we'll never change it 13 | if (OldHashCode.HasValue) 14 | return OldHashCode.Value; 15 | 16 | // The default of string is NULL not string.Empty 17 | bool thisIsTransient = typeof(T) == typeof(string) ? Equals(Id, string.Empty) : Equals(Id, default(T)); 18 | 19 | // When this instance is transient, we use the base GetHashCode() 20 | // and remember it, so an instance can NEVER change its hash code. 21 | if (thisIsTransient) 22 | { 23 | OldHashCode = base.GetHashCode(); 24 | return OldHashCode.Value; 25 | } 26 | return Id.GetHashCode(); 27 | } 28 | 29 | public override bool Equals(object obj) 30 | { 31 | AbstractDomainEntity other = obj as AbstractDomainEntity; 32 | if (other == null) 33 | return false; 34 | 35 | // handle the case of comparing two NEW objects 36 | bool otherIsTransient = typeof(T) == typeof(string) ? Equals(other.Id, string.Empty) : Equals(other.Id, default(T)); 37 | bool thisIsTransient = typeof(T) == typeof(string) ? Equals(Id, string.Empty) : Equals(Id, default(T)); 38 | if (otherIsTransient && thisIsTransient) 39 | return ReferenceEquals(other, this); 40 | 41 | return other.Id.Equals(Id); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Email.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain 2 | { 3 | public class Email 4 | { 5 | public string Subject { get; set; } 6 | public string Body { get; set; } 7 | 8 | public Email() 9 | { 10 | Subject = string.Empty; 11 | Body = string.Empty; 12 | } 13 | 14 | public Email(string subject, string body) 15 | : this() 16 | { 17 | Subject = subject; 18 | Body = body; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IAbstractDomainEntity.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain.Interfaces 2 | { 3 | public interface IAbstractDomainEntity 4 | { 5 | T Id { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IClientErrorDao.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain.Interfaces 2 | { 3 | public interface IClientErrorDao : IDao 4 | { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IClientErrorManager.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain.Interfaces 2 | { 3 | public interface IClientErrorManager 4 | { 5 | void Save(ClientError clientError); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IDao.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain.Interfaces 2 | { 3 | public interface IDao 4 | { 5 | T Merge(T entity); 6 | void Save(T entity); 7 | void Update(T entity); 8 | void Delete(T entity); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IDaoFactory.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | 3 | namespace Streamus_Web_API.Domain.Interfaces 4 | { 5 | /// 6 | /// Provides an interface for retrieving DAO objects 7 | /// 8 | public interface IDaoFactory 9 | { 10 | IClientErrorDao GetErrorDao(ISession session); 11 | IPlaylistDao GetPlaylistDao(ISession session); 12 | IPlaylistItemDao GetPlaylistItemDao(ISession session); 13 | IShareCodeDao GetShareCodeDao(ISession session); 14 | IUserDao GetUserDao(ISession session); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IEmailManager.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain.Interfaces 2 | { 3 | public interface IEmailManager 4 | { 5 | void SendEmail(Email email); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IManagerFactory.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | 3 | namespace Streamus_Web_API.Domain.Interfaces 4 | { 5 | public interface IManagerFactory 6 | { 7 | IClientErrorManager GetErrorManager(ISession session); 8 | IPlaylistItemManager GetPlaylistItemManager(ISession session); 9 | IPlaylistManager GetPlaylistManager(ISession session); 10 | IShareCodeManager GetShareCodeManager(ISession session); 11 | IUserManager GetUserManager(ISession session); 12 | IEmailManager GetEmailManager(); 13 | } 14 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IPlaylistDao.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Streamus_Web_API.Domain.Interfaces 4 | { 5 | public interface IPlaylistDao : IDao 6 | { 7 | Playlist Get(Guid id); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IPlaylistItemDao.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Streamus_Web_API.Domain.Interfaces 4 | { 5 | public interface IPlaylistItemDao : IDao 6 | { 7 | PlaylistItem Get(Guid id); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IPlaylistItemManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Streamus_Web_API.Domain.Interfaces 5 | { 6 | public interface IPlaylistItemManager 7 | { 8 | PlaylistItem Get(Guid id); 9 | void Delete(Guid itemId); 10 | void Save(IEnumerable playlistItems); 11 | void Save(PlaylistItem playlistItem); 12 | void Update(PlaylistItem playlistItem); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IPlaylistManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Streamus_Web_API.Domain.Interfaces 4 | { 5 | public interface IPlaylistManager 6 | { 7 | Playlist Get(Guid id); 8 | Playlist CopyAndSave(Guid id); 9 | void Save(Playlist playlist); 10 | void Update(Playlist playlist); 11 | void Delete(Guid id); 12 | } 13 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IShareCodeDao.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain.Interfaces 2 | { 3 | public interface IShareCodeDao : IDao 4 | { 5 | ShareCode GetByShortIdAndEntityTitle(string shareCodeShortId, string urlFriendlyEntityTitle); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IShareCodeManager.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain.Interfaces 2 | { 3 | public interface IShareCodeManager 4 | { 5 | ShareCode GetByShortIdAndEntityTitle(string shareCodeShortId, string urlFriendlyEntityTitle); 6 | ShareCode GetShareCode(IShareableEntity shareableEntity); 7 | void Save(ShareCode shareCode); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IShareableEntity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Streamus_Web_API.Domain.Interfaces 4 | { 5 | public interface IShareableEntity 6 | { 7 | Guid Id { get; set; } 8 | string GetUrlFriendlyTitle(); 9 | string GetShortId(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IUserDao.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Streamus_Web_API.Domain.Interfaces 5 | { 6 | public interface IUserDao : IDao 7 | { 8 | User Get(Guid id); 9 | User GetByGooglePlusId(string googlePlusId); 10 | IList GetAllByGooglePlusId(string googlePlusId); 11 | void UpdateGooglePlusIds(IList ids, string googlePlusId); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Interfaces/IUserManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Streamus_Web_API.Domain.Interfaces 4 | { 5 | public interface IUserManager 6 | { 7 | /// 8 | /// Creates a new User and saves it to the DB. As a side effect, also creates a new, empty 9 | /// Playlist and also saves it to the DB. 10 | /// 11 | /// The created user with a generated GUID 12 | User CreateUser(string googlePlusId = ""); 13 | 14 | void Save(User user); 15 | void Update(User user); 16 | User MergeByGooglePlusId(Guid id, string googlePlusId); 17 | User MergeAllByGooglePlusId(string googlePlusId); 18 | 19 | User Get(Guid userId); 20 | User GetByGooglePlusId(string googlePlusId); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Managers/ClientErrorManager.cs: -------------------------------------------------------------------------------- 1 | using log4net; 2 | using Streamus_Web_API.Domain.Interfaces; 3 | using System; 4 | 5 | namespace Streamus_Web_API.Domain.Managers 6 | { 7 | /// 8 | /// Provides a common spot for methods against ClientErrors which require transactions (Creating, Updating, Deleting) 9 | /// 10 | public class ClientErrorManager : StreamusManager, IClientErrorManager 11 | { 12 | private IClientErrorDao ClientErrorDao { get; set; } 13 | 14 | public ClientErrorManager(ILog logger, IClientErrorDao clientErrorDao) 15 | : base(logger) 16 | { 17 | ClientErrorDao = clientErrorDao; 18 | } 19 | 20 | public void Save(ClientError clientError) 21 | { 22 | try 23 | { 24 | clientError.ValidateAndThrow(); 25 | ClientErrorDao.Save(clientError); 26 | } 27 | catch (Exception exception) 28 | { 29 | Logger.Error(exception); 30 | throw; 31 | } 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Managers/EmailManager.cs: -------------------------------------------------------------------------------- 1 | using log4net; 2 | using Streamus_Web_API.Domain.Interfaces; 3 | using System; 4 | using System.Configuration; 5 | using System.IO; 6 | using System.Net; 7 | using System.Net.Mail; 8 | using System.Web; 9 | using NHibernate; 10 | 11 | namespace Streamus_Web_API.Domain.Managers 12 | { 13 | public class EmailManager : StreamusManager, IEmailManager 14 | { 15 | public EmailManager(ILog logger) 16 | : base(logger) 17 | { 18 | } 19 | 20 | public void SendEmail(Email email) 21 | { 22 | try 23 | { 24 | MailAddress fromAddress = new MailAddress("admin@streamus.com", "Sean Anderson"); 25 | MailAddress toAddress = new MailAddress("admin@streamus.com", "Sean Anderson"); 26 | string password = ConfigurationManager.AppSettings["emailPassword"]; 27 | 28 | SmtpClient smtpClient = new SmtpClient 29 | { 30 | Host = "smtp.gmail.com", 31 | Port = 587, 32 | EnableSsl = true, 33 | DeliveryMethod = SmtpDeliveryMethod.Network, 34 | UseDefaultCredentials = false, 35 | Credentials = new NetworkCredential(fromAddress.Address, password) 36 | }; 37 | 38 | using (MailMessage mailMessage = new MailMessage(fromAddress, toAddress) 39 | { 40 | Subject = email.Subject, 41 | Body = email.Body 42 | }) 43 | { 44 | smtpClient.Send(mailMessage); 45 | } 46 | } 47 | catch (Exception exception) 48 | { 49 | Logger.Error(exception); 50 | throw; 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Managers/PlaylistItemManager.cs: -------------------------------------------------------------------------------- 1 | using log4net; 2 | using Streamus_Web_API.Domain.Interfaces; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace Streamus_Web_API.Domain.Managers 8 | { 9 | public class PlaylistItemManager : StreamusManager, IPlaylistItemManager 10 | { 11 | private IPlaylistItemDao PlaylistItemDao { get; set; } 12 | 13 | public PlaylistItemManager(ILog logger, IPlaylistItemDao playlistItemDao) 14 | : base(logger) 15 | { 16 | PlaylistItemDao = playlistItemDao; 17 | } 18 | 19 | public PlaylistItem Get(Guid id) 20 | { 21 | PlaylistItem playlistItem; 22 | 23 | try 24 | { 25 | playlistItem = PlaylistItemDao.Get(id); 26 | } 27 | catch (Exception exception) 28 | { 29 | Logger.Error(exception); 30 | throw; 31 | } 32 | 33 | return playlistItem; 34 | } 35 | 36 | public void Delete(Guid itemId) 37 | { 38 | try 39 | { 40 | PlaylistItem playlistItem = Get(itemId); 41 | playlistItem.Playlist.Items.Remove(playlistItem); 42 | PlaylistItemDao.Delete(playlistItem); 43 | } 44 | catch (Exception exception) 45 | { 46 | Logger.Error(exception); 47 | throw; 48 | } 49 | } 50 | 51 | public void Update(PlaylistItem playlistItem) 52 | { 53 | try 54 | { 55 | DoUpdate(playlistItem); 56 | } 57 | catch (Exception exception) 58 | { 59 | Logger.Error(exception); 60 | throw; 61 | } 62 | } 63 | 64 | public void Save(PlaylistItem playlistItem) 65 | { 66 | try 67 | { 68 | DoSave(playlistItem); 69 | } 70 | catch (Exception exception) 71 | { 72 | Logger.Error(exception); 73 | throw; 74 | } 75 | } 76 | 77 | public void Save(IEnumerable playlistItems) 78 | { 79 | try 80 | { 81 | var playlistItemList = playlistItems.ToList(); 82 | playlistItemList.ForEach(DoSave); 83 | } 84 | catch (Exception exception) 85 | { 86 | Logger.Error(exception); 87 | throw; 88 | } 89 | } 90 | 91 | /// 92 | /// This is the work for saving a PlaylistItem without the Transaction wrapper. 93 | /// 94 | private void DoSave(PlaylistItem playlistItem) 95 | { 96 | playlistItem.ValidateAndThrow(); 97 | 98 | PlaylistItemDao.Save(playlistItem); 99 | } 100 | 101 | private void DoUpdate(PlaylistItem playlistItem) 102 | { 103 | playlistItem.ValidateAndThrow(); 104 | 105 | PlaylistItemDao.Update(playlistItem); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Managers/ShareCodeManager.cs: -------------------------------------------------------------------------------- 1 | using log4net; 2 | using Streamus_Web_API.Domain.Interfaces; 3 | using System; 4 | 5 | namespace Streamus_Web_API.Domain.Managers 6 | { 7 | public class ShareCodeManager : StreamusManager, IShareCodeManager 8 | { 9 | private IShareCodeDao ShareCodeDao { get; set; } 10 | 11 | public ShareCodeManager(ILog logger, IShareCodeDao shareCodeDao) 12 | : base(logger) 13 | { 14 | ShareCodeDao = shareCodeDao; 15 | } 16 | 17 | public ShareCode GetByShortIdAndEntityTitle(string shareCodeShortId, string urlFriendlyEntityTitle) 18 | { 19 | ShareCode shareCode; 20 | 21 | try 22 | { 23 | shareCode = ShareCodeDao.GetByShortIdAndEntityTitle(shareCodeShortId, urlFriendlyEntityTitle); 24 | 25 | if (shareCode == null) 26 | throw new ApplicationException("Unable to locate shareCode in database."); 27 | 28 | if (shareCode.EntityType != EntityType.Playlist) 29 | throw new ApplicationException("Expected shareCode to have entityType of Playlist"); 30 | } 31 | catch (Exception exception) 32 | { 33 | Logger.Error(exception); 34 | throw; 35 | } 36 | 37 | return shareCode; 38 | } 39 | 40 | public ShareCode GetShareCode(IShareableEntity shareableEntity) 41 | { 42 | ShareCode shareCode; 43 | 44 | try 45 | { 46 | shareCode = new ShareCode(shareableEntity); 47 | DoSave(shareCode); 48 | } 49 | catch (Exception exception) 50 | { 51 | Logger.Error(exception); 52 | throw; 53 | } 54 | 55 | return shareCode; 56 | } 57 | 58 | public void Save(ShareCode shareCode) 59 | { 60 | try 61 | { 62 | DoSave(shareCode); 63 | } 64 | catch (Exception exception) 65 | { 66 | Logger.Error(exception); 67 | throw; 68 | } 69 | } 70 | 71 | /// 72 | /// This is the work for saving a ShareCode without the Transaction wrapper. 73 | /// 74 | private void DoSave(ShareCode shareCode) 75 | { 76 | shareCode.ValidateAndThrow(); 77 | ShareCodeDao.Save(shareCode); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Managers/StreamusManager.cs: -------------------------------------------------------------------------------- 1 | using log4net; 2 | 3 | namespace Streamus_Web_API.Domain.Managers 4 | { 5 | public abstract class StreamusManager 6 | { 7 | protected readonly ILog Logger; 8 | 9 | protected StreamusManager(ILog logger) 10 | { 11 | Logger = logger; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Managers/StreamusManagerFactory.cs: -------------------------------------------------------------------------------- 1 | using NHibernate; 2 | using log4net; 3 | using Streamus_Web_API.Domain.Interfaces; 4 | using System; 5 | 6 | namespace Streamus_Web_API.Domain.Managers 7 | { 8 | public class StreamusManagerFactory : IManagerFactory 9 | { 10 | private readonly ILog _logger; 11 | private readonly IDaoFactory _daoFactory; 12 | private IClientErrorManager _clientErrorManager; 13 | private IPlaylistItemManager _playlistItemManager; 14 | private IPlaylistManager _playlistManager; 15 | private IShareCodeManager _shareCodeManager; 16 | private IUserManager _userManager; 17 | private IEmailManager _emailManager; 18 | 19 | public StreamusManagerFactory(ILog logger, IDaoFactory daoFactory) 20 | { 21 | if (logger == null) throw new NullReferenceException("logger"); 22 | if (daoFactory == null) throw new NullReferenceException("daoFactory"); 23 | 24 | _logger = logger; 25 | _daoFactory = daoFactory; 26 | } 27 | 28 | public IClientErrorManager GetErrorManager(ISession session) 29 | { 30 | return _clientErrorManager ?? (_clientErrorManager = new ClientErrorManager(_logger, _daoFactory.GetErrorDao(session))); 31 | } 32 | 33 | public IPlaylistItemManager GetPlaylistItemManager(ISession session) 34 | { 35 | return _playlistItemManager ?? (_playlistItemManager = new PlaylistItemManager(_logger, _daoFactory.GetPlaylistItemDao(session))); 36 | } 37 | 38 | public IPlaylistManager GetPlaylistManager(ISession session) 39 | { 40 | return _playlistManager ?? (_playlistManager = new PlaylistManager(_logger, _daoFactory.GetPlaylistDao(session))); 41 | } 42 | 43 | public IShareCodeManager GetShareCodeManager(ISession session) 44 | { 45 | return _shareCodeManager ?? (_shareCodeManager = new ShareCodeManager(_logger, _daoFactory.GetShareCodeDao(session))); 46 | } 47 | 48 | public IUserManager GetUserManager(ISession session) 49 | { 50 | return _userManager ?? (_userManager = new UserManager(_logger, _daoFactory.GetUserDao(session))); 51 | } 52 | 53 | public IEmailManager GetEmailManager() 54 | { 55 | return _emailManager ?? (_emailManager = new EmailManager(_logger)); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/PlaylistItem.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | using Streamus_Web_API.Domain.Validators; 3 | using System; 4 | 5 | namespace Streamus_Web_API.Domain 6 | { 7 | public class PlaylistItem : AbstractDomainEntity 8 | { 9 | public virtual Playlist Playlist { get; set; } 10 | public virtual double Sequence { get; set; } 11 | 12 | public virtual string VideoId { get; set; } 13 | public virtual VideoType VideoType { get; set; } 14 | public virtual string VideoTitle { get; set; } 15 | public virtual int Duration { get; set; } 16 | public virtual string Author { get; set; } 17 | 18 | // Client ID is used to associate a DTO with a client-side entity which wasn't saved before sending to the server. 19 | public virtual string Cid { get; set; } 20 | 21 | public const int MaxAuthorLength = 100; 22 | 23 | // The longest YouTube ID currently is 11, but I'm doing 25 just in case that grows in the future. 24 | public const int MaxVideoIdLength = 25; 25 | // The longest VideoType is currently 10, SoundCloud, but I'm doing 25 just in case that grows in the future. 26 | public const int MaxVideoTypeLength = 25; 27 | public const int MaxVideoTitleLength = 255; 28 | 29 | public PlaylistItem() 30 | { 31 | Id = Guid.Empty; 32 | Sequence = -1; 33 | VideoId = string.Empty; 34 | Author = string.Empty; 35 | VideoType = VideoType.None; 36 | VideoTitle = string.Empty; 37 | Cid = string.Empty; 38 | } 39 | 40 | public PlaylistItem(string cid, string videoId, VideoType videoType, string videoTitle, int duration, string author) 41 | : this() 42 | { 43 | VideoId = videoId; 44 | VideoType = videoType; 45 | VideoTitle = videoTitle; 46 | Duration = duration; 47 | Author = author; 48 | Cid = cid; 49 | } 50 | 51 | public PlaylistItem(PlaylistItem playlistItem) 52 | : this() 53 | { 54 | VideoId = playlistItem.VideoId; 55 | VideoType = playlistItem.VideoType; 56 | VideoTitle = playlistItem.VideoTitle; 57 | Author = playlistItem.Author; 58 | Cid = playlistItem.Cid; 59 | Duration = playlistItem.Duration; 60 | Sequence = playlistItem.Sequence; 61 | } 62 | 63 | public PlaylistItem(Guid id, string cid, string videoId, VideoType videoType, string videoTitle, int duration, string author) 64 | : this(cid, videoId, videoType, videoTitle, duration, author) 65 | { 66 | Id = id; 67 | } 68 | 69 | public virtual void ValidateAndThrow() 70 | { 71 | var validator = new PlaylistItemValidator(); 72 | validator.ValidateAndThrow(this); 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/ShareCode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentValidation; 3 | using Streamus_Web_API.Domain.Interfaces; 4 | using Streamus_Web_API.Domain.Validators; 5 | 6 | namespace Streamus_Web_API.Domain 7 | { 8 | public enum EntityType 9 | { 10 | None = -1, 11 | Playlist = 0 12 | } 13 | 14 | public class ShareCode : AbstractDomainEntity 15 | { 16 | public virtual EntityType EntityType { get; set; } 17 | public virtual Guid EntityId { get; set; } 18 | public virtual string ShortId { get; set; } 19 | public virtual string UrlFriendlyEntityTitle { get; set; } 20 | 21 | public ShareCode() 22 | { 23 | Id = Guid.Empty; 24 | EntityId = Guid.Empty; 25 | EntityType = EntityType.None; 26 | ShortId = string.Empty; 27 | UrlFriendlyEntityTitle = string.Empty; 28 | } 29 | 30 | public ShareCode(IShareableEntity shareableEntity) 31 | : this() 32 | { 33 | if (!(shareableEntity is Playlist)) 34 | throw new NotSupportedException("Only Playlists are shareable currently."); 35 | 36 | EntityType = EntityType.Playlist; 37 | EntityId = shareableEntity.Id; 38 | UrlFriendlyEntityTitle = shareableEntity.GetUrlFriendlyTitle(); 39 | ShortId = shareableEntity.GetShortId(); 40 | } 41 | 42 | public virtual void ValidateAndThrow() 43 | { 44 | var validator = new ShareCodeValidator(); 45 | validator.ValidateAndThrow(this); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/User.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using FluentValidation; 5 | using Streamus_Web_API.Domain.Validators; 6 | 7 | namespace Streamus_Web_API.Domain 8 | { 9 | public class User : AbstractDomainEntity 10 | { 11 | public virtual string GooglePlusId { get; set; } 12 | // Use interfaces so NHibernate can inject with its own collection implementation. 13 | public virtual ICollection Playlists { get; set; } 14 | public virtual string Language { get; set; } 15 | 16 | // GooglePlusID is usually a number, but, in some instances, can be a gmail address which is maximum of 75 characters, but doing 100 to be safe. 17 | public const int MaxGooglePlusIdLength = 100; 18 | public const int MaxLanguageLength = 10; 19 | 20 | public User() 21 | { 22 | GooglePlusId = string.Empty; 23 | Playlists = new List(); 24 | Language = string.Empty; 25 | 26 | // A user should always have at least one Playlist. 27 | CreateAndAddPlaylist(); 28 | } 29 | 30 | public virtual Playlist CreateAndAddPlaylist() 31 | { 32 | var playlist = new Playlist("New Playlist") 33 | { 34 | User = this 35 | }; 36 | 37 | AddPlaylist(playlist); 38 | 39 | return playlist; 40 | } 41 | 42 | public virtual void RemovePlaylist(Playlist playlist) 43 | { 44 | Playlists.Remove(playlist); 45 | } 46 | 47 | public virtual void ValidateAndThrow() 48 | { 49 | var validator = new UserValidator(); 50 | validator.ValidateAndThrow(this); 51 | } 52 | 53 | public virtual void AddPlaylist(Playlist playlist) 54 | { 55 | // Client might not set the sequence number. 56 | if (playlist.Sequence < 0) 57 | { 58 | if (Playlists.Any()) 59 | { 60 | playlist.Sequence = Playlists.OrderBy(i => i.Sequence).Last().Sequence + 10000; 61 | } 62 | else 63 | { 64 | playlist.Sequence = 10000; 65 | } 66 | } 67 | 68 | playlist.User = this; 69 | Playlists.Add(playlist); 70 | } 71 | 72 | public virtual void MergeUser(User user) 73 | { 74 | foreach (Playlist playlist in user.Playlists.Where(p => p.Items.Count > 0)) 75 | { 76 | AddPlaylist(new Playlist(playlist)); 77 | } 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Streamus Web API/Domain/Validators/ClientErrorValidator.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | 3 | namespace Streamus_Web_API.Domain.Validators 4 | { 5 | public class ClientErrorValidator : AbstractValidator 6 | { 7 | public ClientErrorValidator() 8 | { 9 | RuleFor(clientError => clientError.InstanceId).NotNull(); 10 | RuleFor(clientError => clientError.Message).NotNull().Length(0, ClientError.MaxMessageLength); 11 | RuleFor(clientError => clientError.LineNumber).GreaterThan(ClientError.LineNumberDefault); 12 | RuleFor(clientError => clientError.ClientVersion).NotNull().Length(0, ClientError.MaxClientVersionLength); 13 | RuleFor(clientError => clientError.BrowserVersion).NotNull(); 14 | RuleFor(clientError => clientError.Url).NotNull().Length(0, ClientError.MaxUrlLength); 15 | RuleFor(clientError => clientError.Stack).NotNull().Length(0, ClientError.MaxStackLength); 16 | RuleFor(clientError => clientError.TimeOccurred).NotNull(); 17 | RuleFor(clientError => clientError.UserId).NotNull(); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Validators/PlaylistItemValidator.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | 3 | namespace Streamus_Web_API.Domain.Validators 4 | { 5 | public class PlaylistItemValidator : AbstractValidator 6 | { 7 | public PlaylistItemValidator() 8 | { 9 | RuleFor(playlistItem => playlistItem.Playlist).NotNull(); 10 | RuleFor(playlistItem => playlistItem.Sequence).GreaterThanOrEqualTo(0); 11 | RuleFor(playlistItem => playlistItem.VideoId).NotNull().Length(1, 255); 12 | RuleFor(playlistItem => playlistItem.VideoType).NotEqual(VideoType.None); 13 | RuleFor(playlistItem => playlistItem.VideoTitle).NotNull().Length(1, 255); 14 | RuleFor(playlistItem => playlistItem.Duration).GreaterThan(0); 15 | RuleFor(playlistItem => playlistItem.Author).NotNull().Length(1, 255); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Validators/PlaylistValidator.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | 3 | namespace Streamus_Web_API.Domain.Validators 4 | { 5 | public class PlaylistValidator : AbstractValidator 6 | { 7 | public PlaylistValidator() 8 | { 9 | RuleFor(playlist => playlist.Title).NotNull().Length(1, 255); 10 | // When sharing a playlist the user can be null and, in that case, it's OK for the sequence to not be set. Otherwise it needs to be. 11 | RuleFor(playlist => playlist.Sequence).GreaterThanOrEqualTo(0).When(playlist => playlist.User != null); 12 | RuleFor(playlist => playlist.Items).NotNull(); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Validators/ShareCodeValidator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentValidation; 3 | 4 | namespace Streamus_Web_API.Domain.Validators 5 | { 6 | public class ShareCodeValidator : AbstractValidator 7 | { 8 | public ShareCodeValidator() 9 | { 10 | RuleFor(shareCode => shareCode.EntityType).NotEqual(EntityType.None); 11 | RuleFor(shareCode => shareCode.EntityId).NotEqual(Guid.Empty); 12 | RuleFor(shareCode => shareCode.ShortId).Length(12); 13 | RuleFor(shareCode => shareCode.UrlFriendlyEntityTitle.Length).GreaterThan(0); 14 | RuleFor(shareCode => shareCode.UrlFriendlyEntityTitle.IndexOf(" ")) 15 | .Equal(-1) 16 | .WithName("UrlFriendlyEntityTitle"); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/Validators/UserValidator.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | 3 | namespace Streamus_Web_API.Domain.Validators 4 | { 5 | public class UserValidator : AbstractValidator 6 | { 7 | public UserValidator() 8 | { 9 | RuleFor(user => user.GooglePlusId).NotNull(); 10 | RuleFor(user => user.Playlists).NotNull(); 11 | RuleFor(user => user.Language).NotNull().Length(0, 10); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Streamus Web API/Domain/VideoType.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Domain 2 | { 3 | public enum VideoType 4 | { 5 | None = 0, 6 | YouTube = 1, 7 | SoundCloud = 2 8 | } 9 | } -------------------------------------------------------------------------------- /Streamus Web API/Dto/ClientErrorDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AutoMapper; 3 | using Streamus_Web_API.Domain; 4 | 5 | namespace Streamus_Web_API.Dto 6 | { 7 | public class ClientErrorDto 8 | { 9 | public string InstanceId { get; set; } 10 | public string Message { get; set; } 11 | public int LineNumber { get; set; } 12 | public string Url { get; set; } 13 | public string ClientVersion { get; set; } 14 | public string OperatingSystem { get; set; } 15 | public string Architecture { get; set; } 16 | public string Stack { get; set; } 17 | public string BrowserVersion { get; set; } 18 | public Guid UserId { get; set; } 19 | 20 | public ClientErrorDto() 21 | { 22 | InstanceId = string.Empty; 23 | Message = string.Empty; 24 | Architecture = string.Empty; 25 | OperatingSystem = string.Empty; 26 | LineNumber = -1; 27 | Url = string.Empty; 28 | ClientVersion = string.Empty; 29 | Stack = string.Empty; 30 | BrowserVersion = string.Empty; 31 | UserId = Guid.Empty; 32 | } 33 | 34 | public static ClientErrorDto Create(ClientError clientError) 35 | { 36 | ClientErrorDto clientErrorDto = Mapper.Map(clientError); 37 | return clientErrorDto; 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /Streamus Web API/Dto/ContactDto.cs: -------------------------------------------------------------------------------- 1 | namespace Streamus_Web_API.Dto 2 | { 3 | public class ContactDto 4 | { 5 | public string Name { get; set; } 6 | public string Email { get; set; } 7 | public string Message { get; set; } 8 | 9 | public ContactDto() 10 | { 11 | Name = string.Empty; 12 | Email = string.Empty; 13 | Message = string.Empty; 14 | } 15 | 16 | public ContactDto(string name, string email, string message) 17 | : this() 18 | { 19 | Name = name; 20 | Email = email; 21 | Message = message; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Streamus Web API/Dto/CopyPlaylistRequestDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Streamus_Web_API.Dto 4 | { 5 | public class CopyPlaylistRequestDto 6 | { 7 | public Guid UserId { get; set; } 8 | public Guid PlaylistId { get; set; } 9 | 10 | public CopyPlaylistRequestDto() 11 | { 12 | 13 | } 14 | 15 | public CopyPlaylistRequestDto(Guid userId, Guid playlistId) 16 | { 17 | UserId = userId; 18 | PlaylistId = playlistId; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Streamus Web API/Dto/PlaylistDto.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Streamus_Web_API.Domain; 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace Streamus_Web_API.Dto 7 | { 8 | public class PlaylistDto 9 | { 10 | public Guid Id { get; set; } 11 | public string Title { get; set; } 12 | public Guid UserId { get; set; } 13 | public List Items { get; set; } 14 | public double? Sequence { get; set; } 15 | 16 | public PlaylistDto() 17 | { 18 | Id = Guid.Empty; 19 | Items = new List(); 20 | } 21 | 22 | public static PlaylistDto Create(Playlist playlist) 23 | { 24 | PlaylistDto playlistDto = Mapper.Map(playlist); 25 | return playlistDto; 26 | } 27 | 28 | public void SetPatchableProperties(Playlist playlist) 29 | { 30 | if (Title != null) 31 | playlist.Title = Title; 32 | 33 | if (Sequence.HasValue) 34 | playlist.Sequence = (double)Sequence; 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Streamus Web API/Dto/PlaylistItemDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using AutoMapper; 4 | using Streamus_Web_API.Domain; 5 | 6 | namespace Streamus_Web_API.Dto 7 | { 8 | public class PlaylistItemDto 9 | { 10 | public Guid PlaylistId { get; set; } 11 | public Guid Id { get; set; } 12 | public double? Sequence { get; set; } 13 | // Client ID is used to associate a DTO with a client-side entity which wasn't saved before sending to the server. 14 | public string Cid { get; set; } 15 | public VideoDto Video { get; set; } 16 | public VideoDto Song { get; set; } 17 | 18 | public PlaylistItemDto() 19 | { 20 | Id = Guid.Empty; 21 | Cid = string.Empty; 22 | } 23 | 24 | public static PlaylistItemDto Create(PlaylistItem playlistItem) 25 | { 26 | PlaylistItemDto playlistItemDto = Mapper.Map(playlistItem); 27 | return playlistItemDto; 28 | } 29 | 30 | public static List Create(IEnumerable playlistItems) 31 | { 32 | List playlistItemDtos = Mapper.Map, List>(playlistItems); 33 | return playlistItemDtos; 34 | } 35 | 36 | public void SetPatchableProperties(PlaylistItem playlistItem) 37 | { 38 | if (Sequence.HasValue) 39 | playlistItem.Sequence = (double)Sequence; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /Streamus Web API/Dto/ShareCodeDto.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using System; 3 | using Streamus_Web_API.Domain; 4 | 5 | namespace Streamus_Web_API.Dto 6 | { 7 | public class ShareCodeDto 8 | { 9 | public Guid Id { get; set; } 10 | public EntityType EntityType { get; set; } 11 | public Guid EntityId { get; set; } 12 | public string ShortId { get; set; } 13 | public string UrlFriendlyEntityTitle { get; set; } 14 | 15 | public ShareCodeDto() 16 | { 17 | Id = Guid.Empty; 18 | EntityId = Guid.Empty; 19 | EntityType = EntityType.None; 20 | ShortId = string.Empty; 21 | UrlFriendlyEntityTitle = string.Empty; 22 | } 23 | 24 | public static ShareCodeDto Create(ShareCode shareCode) 25 | { 26 | ShareCodeDto shareCodeDto = Mapper.Map(shareCode); 27 | return shareCodeDto; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Streamus Web API/Dto/UserDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using AutoMapper; 4 | using Streamus_Web_API.Domain; 5 | 6 | namespace Streamus_Web_API.Dto 7 | { 8 | public class UserDto 9 | { 10 | public Guid Id { get; set; } 11 | public string GooglePlusId { get; set; } 12 | public List Playlists { get; set; } 13 | public string Language { get; set; } 14 | 15 | public UserDto() 16 | { 17 | Playlists = new List(); 18 | } 19 | 20 | public static UserDto Create(User user) 21 | { 22 | UserDto userDto = Mapper.Map(user); 23 | return userDto; 24 | } 25 | 26 | public void SetPatchableProperties(User user) 27 | { 28 | if (GooglePlusId != null) 29 | user.GooglePlusId = GooglePlusId; 30 | 31 | if (Language != null) 32 | user.Language = Language; 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Streamus Web API/Dto/VideoDto.cs: -------------------------------------------------------------------------------- 1 | using Streamus_Web_API.Domain; 2 | 3 | namespace Streamus_Web_API.Dto 4 | { 5 | public class VideoDto 6 | { 7 | public string Id { get; set; } 8 | public VideoType Type { get; set; } 9 | public string Title { get; set; } 10 | public int Duration { get; set; } 11 | public string Author { get; set; } 12 | 13 | public VideoDto() 14 | { 15 | Id = string.Empty; 16 | Title = string.Empty; 17 | Author = string.Empty; 18 | Type = VideoType.None; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Streamus Web API/Global.asax: -------------------------------------------------------------------------------- 1 | <%@ Application Codebehind="Global.asax.cs" Inherits="Streamus_Web_API.StreamusWebApi" Language="C#" %> 2 | -------------------------------------------------------------------------------- /Streamus Web API/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Streamus_Web_API")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Streamus_Web_API")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("9443be63-07db-4533-854f-295367b5f437")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /Streamus Web API/Properties/PublishProfiles/aws-server.pubxml: -------------------------------------------------------------------------------- 1 |  2 | 6 | 7 | 8 | MSDeploy 9 | Release 10 | Any CPU 11 | 12 | True 13 | False 14 | aws-server.streamus.com 15 | Default Web Site/Streamus 16 | 17 | True 18 | WMSVC 19 | True 20 | Streamus 21 | <_SavePWD>False 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Streamus Web API/Web.Debug.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 17 | 18 | 29 | 30 | -------------------------------------------------------------------------------- /Streamus Web API/Web.Release.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 17 | 18 | 19 | 30 | 31 | -------------------------------------------------------------------------------- /Streamus Web API/Web.config: -------------------------------------------------------------------------------- 1 |  2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Streamus Web API/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Streamus.Tests/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Streamus.Tests/Controller Tests/ErrorControllerTest.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using Streamus.Controllers; 3 | using Streamus.Dto; 4 | using System; 5 | using System.Web.Mvc; 6 | 7 | namespace Streamus.Tests.Controller_Tests 8 | { 9 | [TestFixture] 10 | public class ErrorControllerTest : StreamusTest 11 | { 12 | private ErrorController ErrorController; 13 | 14 | /// 15 | /// This code is only ran once for the given TestFixture. 16 | /// 17 | [TestFixtureSetUp] 18 | public new void TestFixtureSetUp() 19 | { 20 | try 21 | { 22 | ErrorController = new ErrorController(Logger, Session, ManagerFactory); 23 | } 24 | catch (TypeInitializationException exception) 25 | { 26 | throw exception.InnerException; 27 | } 28 | } 29 | 30 | [Test] 31 | public void CreateError_ShortMessage_ErrorCreated() 32 | { 33 | var errorDto = new ErrorDto 34 | { 35 | Message = "Hello World", 36 | ClientVersion = "0.99", 37 | LineNumber = 2 38 | }; 39 | 40 | JsonResult result = ErrorController.Create(errorDto); 41 | var createdErrorDto = (ErrorDto)result.Data; 42 | 43 | Assert.NotNull(createdErrorDto); 44 | } 45 | 46 | [Test] 47 | public void CreateError_LongMessage_MessageTruncatedErrorCreated() 48 | { 49 | var errorDto = new ErrorDto 50 | { 51 | Message = 52 | "Hello World This is a Really Long Message Which is going to be over 255 characters in length when finished which will cause the end result message to be truncated with three periods as to not overflow the database. Can I confirm that this is happening? Thanks", 53 | ClientVersion = "0.99", 54 | LineNumber = 2 55 | }; 56 | 57 | JsonResult result = ErrorController.Create(errorDto); 58 | 59 | var createdErrorDto = (ErrorDto)result.Data; 60 | 61 | Assert.NotNull(createdErrorDto); 62 | Assert.That(createdErrorDto.Message.Length == 255); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /Streamus.Tests/InMemoryDatabaseTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using NHibernate; 7 | using NHibernate.Cfg; 8 | using NHibernate.Dialect; 9 | using NHibernate.Driver; 10 | using NHibernate.Tool.hbm2ddl; 11 | using Environment = NHibernate.Cfg.Environment; 12 | 13 | namespace Streamus.Tests 14 | { 15 | /// 16 | /// http://ayende.com/blog/3983/nhibernate-unit-testing 17 | /// 18 | public class InMemoryDatabaseTest : IDisposable 19 | { 20 | private static Configuration Configuration; 21 | private static ISessionFactory SessionFactory; 22 | protected ISession session; 23 | 24 | public InMemoryDatabaseTest(Assembly assemblyContainingMapping) 25 | { 26 | if (Configuration == null) 27 | { 28 | Configuration = new Configuration() 29 | .SetProperty(Environment.ReleaseConnections, "on_close") 30 | .SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName) 31 | .SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName) 32 | .SetProperty(Environment.ConnectionString, "data source=:memory:") 33 | .AddAssembly(assemblyContainingMapping); 34 | 35 | SessionFactory = Configuration.BuildSessionFactory(); 36 | } 37 | 38 | session = SessionFactory.OpenSession(); 39 | 40 | new SchemaExport(Configuration).Execute(false, true, false); 41 | } 42 | 43 | public void Dispose() 44 | { 45 | session.Dispose(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Streamus.Tests/Manager Tests/PlaylistManagerTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NHibernate; 3 | using NUnit.Framework; 4 | using Streamus.Dao; 5 | using Streamus.Domain; 6 | using Streamus.Domain.Interfaces; 7 | 8 | namespace Streamus.Tests.Manager_Tests 9 | { 10 | [TestFixture] 11 | public class PlaylistManagerTest : StreamusTest 12 | { 13 | private IPlaylistManager PlaylistManager; 14 | 15 | private User User { get; set; } 16 | private Video Video { get; set; } 17 | 18 | /// 19 | /// This code is only ran once for the given TestFixture. 20 | /// 21 | [TestFixtureSetUp] 22 | public new void TestFixtureSetUp() 23 | { 24 | IVideoManager videoManager; 25 | 26 | try 27 | { 28 | PlaylistManager = ManagerFactory.GetPlaylistManager(); 29 | videoManager = ManagerFactory.GetVideoManager(); 30 | } 31 | catch (TypeInitializationException exception) 32 | { 33 | throw exception.InnerException; 34 | } 35 | 36 | User = Helpers.CreateUser(); 37 | Video = Helpers.CreateUnsavedVideoWithId(); 38 | 39 | videoManager.Save(Video); 40 | } 41 | 42 | [Test] 43 | public void Updates() 44 | { 45 | Playlist playlist = User.CreateAndAddPlaylist(); 46 | PlaylistManager.Save(playlist); 47 | 48 | const string newTitle = "Existing Playlist 001"; 49 | PlaylistManager.UpdateTitle(playlist.Id, newTitle); 50 | Assert.AreEqual(playlist.Title, newTitle); 51 | } 52 | 53 | /// 54 | /// Verifies that a Playlist can be deleted properly. 55 | /// 56 | [Test] 57 | public void DeletePlaylist() 58 | { 59 | // Create a new Playlist and write it to the database. 60 | string title = string.Format("New Playlist {0:D4}", User.Playlists.Count); 61 | var playlist = new Playlist(title); 62 | 63 | User.AddPlaylist(playlist); 64 | 65 | PlaylistManager.Save(playlist); 66 | 67 | // Now delete the created Playlist and ensure it is removed. 68 | PlaylistManager.Delete(playlist.Id); 69 | 70 | 71 | bool exceptionEncountered = false; 72 | try 73 | { 74 | Playlist deletedPlaylist = PlaylistManager.Get(playlist.Id); 75 | } 76 | catch (ObjectNotFoundException) 77 | { 78 | exceptionEncountered = true; 79 | } 80 | 81 | Assert.IsTrue(exceptionEncountered); 82 | } 83 | } 84 | } -------------------------------------------------------------------------------- /Streamus.Tests/Manager Tests/ShareCodeManagerTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Streamus.Tests.Manager_Tests 7 | { 8 | // TODO: Test getting a Playlist ShareCode. 9 | public class ShareCodeManagerTest : StreamusTest 10 | { 11 | } 12 | } -------------------------------------------------------------------------------- /Streamus.Tests/Manager Tests/UserManagerTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Streamus.Dao; 4 | using Streamus.Domain; 5 | using Streamus.Domain.Interfaces; 6 | 7 | namespace Streamus.Tests.Manager_Tests 8 | { 9 | [TestFixture] 10 | public class UserManagerTest : StreamusTest 11 | { 12 | private IUserDao UserDao { get; set; } 13 | 14 | /// 15 | /// This code is only ran once for the given TestFixture. 16 | /// 17 | [TestFixtureSetUp] 18 | public new void TestFixtureSetUp() 19 | { 20 | try 21 | { 22 | UserDao = DaoFactory.GetUserDao(); 23 | } 24 | catch (TypeInitializationException exception) 25 | { 26 | throw exception.InnerException; 27 | } 28 | } 29 | 30 | [Test] 31 | public void CreateUser_UserDoesntExist_UserCreated() 32 | { 33 | User user = Helpers.CreateUser(); 34 | 35 | Assert.IsNotNull(user); 36 | Assert.IsNotEmpty(user.Playlists); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /Streamus.Tests/Manager Tests/UserManagerTest.cs.orig: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Streamus.Dao; 4 | using Streamus.Domain; 5 | using Streamus.Domain.Interfaces; 6 | 7 | namespace Streamus.Tests.Manager_Tests 8 | { 9 | [TestFixture] 10 | public class UserManagerTest : StreamusTest 11 | { 12 | private IUserDao UserDao { get; set; } 13 | 14 | /// 15 | /// This code is only ran once for the given TestFixture. 16 | /// 17 | [TestFixtureSetUp] 18 | public new void TestFixtureSetUp() 19 | { 20 | try 21 | { 22 | UserDao = DaoFactory.GetUserDao(); 23 | } 24 | catch (TypeInitializationException exception) 25 | { 26 | throw exception.InnerException; 27 | } 28 | } 29 | 30 | [Test] 31 | public void CreateUser_UserDoesntExist_UserCreated() 32 | { 33 | User user = Helpers.CreateUser(); 34 | 35 | <<<<<<< HEAD 36 | NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); 37 | 38 | User userFromDatabase = UserDao.Get(user.Id); 39 | // Test that the product was successfully inserted 40 | Assert.AreNotSame(user, userFromDatabase); 41 | 42 | Assert.IsNotNull(userFromDatabase); 43 | Assert.IsNotEmpty(userFromDatabase.Playlists); 44 | 45 | NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); 46 | ======= 47 | Assert.IsNotNull(user); 48 | Assert.IsNotEmpty(user.Playlists); 49 | >>>>>>> 16381095067cf126186665c366f47078d4bae461 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Streamus.Tests/PersistanceMappingTests/ErrorMappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentNHibernate.Testing; 3 | using NUnit.Framework; 4 | using Streamus.Dao; 5 | using Streamus.Domain; 6 | 7 | namespace Streamus.Tests.PersistanceMappingTests 8 | { 9 | [TestFixture] 10 | public class ErrorMappingTest 11 | { 12 | [Test] 13 | public void ShouldMap() 14 | { 15 | var sessionFactory = new NHibernateConfiguration().Configure().BuildSessionFactory(); 16 | 17 | using (var session = sessionFactory.OpenSession()) 18 | { 19 | using (session.BeginTransaction()) 20 | { 21 | var sampleOccurenceTime = DateTime.Today; 22 | 23 | new PersistenceSpecification(session) 24 | .CheckProperty(e => e.LineNumber, 1) 25 | .CheckProperty(e => e.Message, "testmessage") 26 | .CheckProperty(e => e.OperatingSystem, "operating system") 27 | .CheckProperty(e => e.TimeOccurred, sampleOccurenceTime) 28 | .CheckProperty(e => e.Url, "url") 29 | .CheckProperty(e => e.Architecture, "astrological") 30 | .VerifyTheMappings(); 31 | } 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Streamus.Tests/PersistanceMappingTests/PlaylistItemMappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Streamus.Dao; 4 | using Streamus.Domain; 5 | 6 | namespace Streamus.Tests.PersistanceMappingTests 7 | { 8 | [TestFixture] 9 | public class PlaylistItemMappingTest 10 | { 11 | [Test] 12 | public void ShouldMap() 13 | { 14 | var sessionFactory = new NHibernateConfiguration().Configure().BuildSessionFactory(); 15 | 16 | using (var session = sessionFactory.OpenSession()) 17 | { 18 | using (var transaction = session.BeginTransaction()) 19 | { 20 | var createdUser = new User {GooglePlusId = "some id?", Name = "user name"}; 21 | session.Save(createdUser); 22 | 23 | var playlist2 = new Playlist("users second playlist") 24 | { 25 | User = createdUser, 26 | Sequence = 200, 27 | }; 28 | 29 | var video = new Video 30 | { 31 | Id = "some id", 32 | Author = "video author", 33 | Duration = 90, 34 | HighDefinition = true, 35 | Title = "my video", 36 | }; 37 | session.Save(video); 38 | var playlistItem = new PlaylistItem 39 | { 40 | Cid = "cid", 41 | Playlist = playlist2, 42 | Video = video, 43 | Sequence = 300, 44 | Title = "My playlist item", 45 | }; 46 | 47 | playlist2.AddItem(playlistItem); 48 | 49 | session.Save(playlist2); 50 | 51 | session.Flush(); 52 | session.Clear(); 53 | 54 | var savedPlaylistItem = session.Get(playlistItem.Id); 55 | 56 | Assert.That(savedPlaylistItem.Title, Is.EqualTo("My playlist item")); 57 | Assert.That(savedPlaylistItem.Id, Is.Not.EqualTo(Guid.Empty)); 58 | Assert.That(savedPlaylistItem.Sequence, Is.EqualTo(300)); 59 | 60 | Assert.That(savedPlaylistItem.Video, Is.EqualTo(playlistItem.Video)); 61 | 62 | transaction.Rollback(); 63 | } 64 | } 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /Streamus.Tests/PersistanceMappingTests/PlaylistMappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | using Streamus.Dao; 4 | using Streamus.Domain; 5 | 6 | namespace Streamus.Tests.PersistanceMappingTests 7 | { 8 | [TestFixture] 9 | public class PlaylistMappingTest 10 | { 11 | [Test] 12 | public void ShouldMap() 13 | { 14 | var sessionFactory = new NHibernateConfiguration().Configure().BuildSessionFactory(); 15 | 16 | using (var session = sessionFactory.OpenSession()) 17 | { 18 | using (var transaction = session.BeginTransaction()) 19 | { 20 | var createdUser = new User {GooglePlusId = "some id?", Name = "user name"}; 21 | 22 | session.Save(createdUser); 23 | 24 | var playlist2 = new Playlist("users second playlist") 25 | { 26 | User = createdUser, 27 | Sequence = 200, 28 | }; 29 | 30 | var playlistItem = new PlaylistItem 31 | { 32 | Cid = "cid", 33 | Playlist = playlist2, 34 | Video = new Video(), 35 | Sequence = 200, 36 | }; 37 | 38 | playlist2.AddItem(playlistItem); 39 | 40 | var playlistId = session.Save(playlist2); 41 | 42 | session.Flush(); 43 | session.Clear(); 44 | 45 | var savedPlaylist = session.Get(playlistId); 46 | 47 | Assert.That(savedPlaylist.Title, Is.EqualTo("users second playlist")); 48 | Assert.That(savedPlaylist.Id, Is.Not.EqualTo(Guid.Empty)); 49 | Assert.That(savedPlaylist.Sequence, Is.EqualTo(200)); 50 | 51 | Assert.That(savedPlaylist.Items, Has.Exactly(1).EqualTo(playlistItem)); 52 | 53 | transaction.Rollback(); 54 | } 55 | } 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /Streamus.Tests/PersistanceMappingTests/UserMappingTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using NHibernate.Linq; 4 | using NUnit.Framework; 5 | using Streamus.Dao; 6 | using Streamus.Domain; 7 | 8 | namespace Streamus.Tests.PersistanceMappingTests 9 | { 10 | [TestFixture] 11 | public class UserMappingTest 12 | { 13 | [Test] 14 | public void ShouldMap() 15 | { 16 | var sessionFactory = new NHibernateConfiguration().Configure().BuildSessionFactory(); 17 | 18 | using (var session = sessionFactory.OpenSession()) 19 | { 20 | using (var transaction = session.BeginTransaction()) 21 | { 22 | var createdUser = new User {GooglePlusId = "some id?", Name = "user name"}; 23 | var playlist = new Playlist("boss songs") {User = createdUser}; 24 | createdUser.AddPlaylist(playlist); 25 | 26 | session.Save(createdUser); 27 | session.Flush(); 28 | session.Clear(); 29 | 30 | var savedUser = session.Get(createdUser.Id); 31 | Assert.That(savedUser.Id, Is.Not.EqualTo(Guid.Empty)); 32 | 33 | Assert.That(savedUser.GooglePlusId, Is.EqualTo(createdUser.GooglePlusId)); 34 | Assert.That(savedUser.Name, Is.EqualTo(createdUser.Name)); 35 | Assert.That(savedUser.Playlists, Has.Count.EqualTo(2)); 36 | 37 | Assert.That(session.Query().Where(p => p.User == savedUser).ToList(), Has.Count.EqualTo(2)); 38 | 39 | transaction.Rollback(); 40 | } 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Streamus.Tests/PersistanceMappingTests/VideoMappingTest.cs: -------------------------------------------------------------------------------- 1 | using FluentNHibernate.Testing; 2 | using NUnit.Framework; 3 | using Streamus.Dao; 4 | using Streamus.Domain; 5 | 6 | namespace Streamus.Tests.PersistanceMappingTests 7 | { 8 | [TestFixture] 9 | public class VideoMappingTest 10 | { 11 | [Test] 12 | public void ShouldMap() 13 | { 14 | var sessionFactory = new NHibernateConfiguration().Configure().BuildSessionFactory(); 15 | 16 | using (var session = sessionFactory.OpenSession()) 17 | { 18 | using (var transaction = session.BeginTransaction()) 19 | { 20 | new PersistenceSpecification