using System; using System.Data.Entity; using System.Linq; using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Exceptions; using Core.Common.Base.Data; namespace Application.Ringtoets.Storage.Converters { /// /// Converter for to and to . /// public static class ProjectEntityConverter { /// /// Gets a new , based on the found in the database. /// /// Database set of . /// A new or null when not found. /// Thrown when is null. public static Project GetProject(IDbSet dbSet) { var entry = dbSet.FirstOrDefault(); return entry == null ? null : ProjectEntityToProject(entry); } /// /// Updates the , based upon the , in the . /// /// Execute .SaveChanges() afterwards to update the storage. /// Database set of . /// to be saved in the database. /// Thrown when or is null. /// When multiple instances are found that refer to . /// When no entities was found that refer to . public static void UpdateProjectEntity(IDbSet dbSet, Project project) { if (project == null) { throw new ArgumentNullException(); } var entry = dbSet.SingleOrDefault(db => db.ProjectEntityId == project.StorageId); if (entry == null) { throw new EntityNotFoundException(); } ProjectToProjectEntity(project, entry); } /// /// Insert the , based upon the , in the . /// /// Execute .SaveChanges() afterwards to update the storage. /// Database set of . /// to be saved in the database. /// Thrown when or is null. public static void InsertProjectEntity(IDbSet dbSet, Project project) { var projectEntity = new ProjectEntity(); ProjectToProjectEntity(project, projectEntity); dbSet.Add(projectEntity); } /// /// Converts to . /// /// The to convert. /// A reference to the to be saved. /// Thrown when or is null. private static void ProjectToProjectEntity(Project project, ProjectEntity projectEntity) { if (project == null || projectEntity == null) { throw new ArgumentNullException(); } projectEntity.Name = project.Name; projectEntity.Description = project.Description; projectEntity.LastUpdated = new DateTime().Ticks; } /// /// Converts to a new instance of . /// /// to convert. /// A new instance of , based on the properties of . private static Project ProjectEntityToProject(ProjectEntity projectEntity) { var project = new Project { StorageId = projectEntity.ProjectEntityId, Name = projectEntity.Name, Description = projectEntity.Description }; return project; } } }