Last active
August 2, 2017 20:53
-
-
Save diegodfsd/43d7f5c0b892a5d554e5ad4984b9e10b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BaseContext<TContext> : DbContext, IDataContext, IQueryDataContext, IUnitOfWork | |
where TContext : DbContext | |
{ | |
public override int SaveChanges() | |
{ | |
return Save(() => | |
{ | |
var affectedRows = base.SaveChanges(); | |
return Task.FromResult(affectedRows); | |
}).Result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ICommandResult Handle(CreateRegistrationCommand command) | |
{ | |
using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions{ IsolationLevel = IsolationLevel.ReadCommitted })) | |
{ | |
var course = _courseRepository | |
.GetCourses() | |
.Published() | |
.Eager(_ => _.Groups) | |
.Eager(_ => _.Groups.Select(g => g.Course)) | |
.ById(command.CourseId); | |
if (course.IsNull()) | |
return CommandResult.BadRequest(ErrorCode.COURSE_NOT_FOUND); | |
var student = _studentRepository.GetStudents().ById(command.UserId); | |
if (student.IsNull()) | |
return CommandResult.BadRequest(ErrorCode.STUDENT_NOT_FOUND); | |
var availableGroup = course.GetAvailableGroup(command.GroupId); | |
if (availableGroup.IsNull()) | |
return CommandResult.BadRequest(ErrorCode.GROUP_NOT_FOUND); | |
var registration = _registrationFactory.CreateRegistration(student, availableGroup); | |
if (registration.Items.IsEmpty()) | |
return CommandResult.BadRequest(ErrorCode.REGISTRATION_INCONSISTENT); | |
_registrationRepository.Save(registration); | |
_unitOfWork.SaveChanges(); | |
scope.Complete(); | |
return CommandResult.Created((new { Id = registration.Id, CourseId = course.Id }).ToDynamic()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Save<T>(T entity) where T : class, IEntity<T>, IAggregateRoot | |
{ | |
var context = GetContext<T>(); | |
//var e = context.Entry(entity); | |
//if (e.State == EntityState.Detached) | |
//{ | |
// context.Set<T>().Attach(entity); | |
// return; | |
//} | |
if (entity.IsPersisted()) { | |
context.Entry(entity).State = EntityState.Modified; | |
} else { | |
context.Set<T>().Add(entity); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Registration : Entity<Registration>, ILoggable, IFreezable, IAggregateRoot | |
{ | |
#region Private Members | |
private ICollection<RegistrationItem> _items = new List<RegistrationItem>(); | |
private RegistrationStatus CurrentStatus { get; set; } | |
#endregion | |
#region Properties | |
public bool IsFrozen { get; private set; } | |
public Student Student { get; protected set; } | |
public Group Group { get; protected set; } | |
public RegistrationState State | |
{ | |
get { return RegistrationState.FromValue(CurrentStatus); } | |
protected set { CurrentStatus = value.Status; } | |
} | |
public DateTime RegisteredAt { get; protected set; } | |
public DateTime? FinishedAt { get; protected set; } | |
public virtual ICollection<RegistrationItem> Items => _items; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RegistrationFactory | |
{ | |
private readonly IContentRepository _contentRepository; | |
public RegistrationFactory(IContentRepository contentRepository) | |
{ | |
_contentRepository = contentRepository; | |
} | |
public Registration CreateRegistration(Student student, Group group) | |
{ | |
if (student.IsNull()) throw new ArgumentNullException("student"); | |
if (group.IsNull()) throw new ArgumentNullException("group"); | |
var registration = new Registration(student, group); | |
var items = group | |
.GetActiveCourseContent() | |
.Select(classContent => RegistrationItem.Create(registration, classContent, _ => CreateSubItems(_, classContent.Content))); | |
registration.AddItems(items); | |
registration.Freeze(); | |
return registration; | |
} | |
private IEnumerable<RegistrationSubItem> CreateSubItems(RegistrationItem item, Content content) | |
{ | |
if (content.Type == ContentType.Scorm) | |
return GetSharableContentItems(item, content); | |
if (content.Type == ContentType.Page) | |
return new[] { new RegistrationSubItem(item, content.Id) }; | |
if (content.Type == ContentType.Form) | |
return new[] { new RegistrationSubItem(item, content.Id) }; | |
return Enumerable.Empty<RegistrationSubItem>(); | |
} | |
private IEnumerable<RegistrationSubItem> GetSharableContentItems(RegistrationItem registrationItem, Content content) | |
{ | |
var contendId = content.Id; | |
var sharableContent = _contentRepository | |
.GetSharableContents() | |
.Eager(_ => _.Organization) | |
.Eager(_ => _.Organization.Activities) | |
.First(_ => _.Content.Id == contendId); | |
return sharableContent | |
.Organization | |
.Activities | |
.Flatten(_ => _.Children) | |
.Distinct() | |
.Select(_ => (new RegistrationSubItem(registrationItem, _.Id)).Tap(trackintItem => AddDefaultParameters(trackintItem, _))); | |
} | |
private void AddDefaultParameters(RegistrationSubItem registrationSubItem, Activity activity) | |
{ | |
registrationSubItem.AddOrUpdateParameter("cmi.core.credit", "credit"); | |
registrationSubItem.AddOrUpdateParameter("cmi.core.lesson_status", "not attempted"); | |
registrationSubItem.AddOrUpdateParameter("cmi.core.entry", "ab-initio"); | |
registrationSubItem.AddOrUpdateParameter("cmi.core.lesson_mode", "normal"); | |
registrationSubItem.AddOrUpdateParameter("cmi.core.exit", "suspend"); | |
registrationSubItem.AddOrUpdateParameter("cmi.student_data.time_limit_action", "continue,no message"); | |
if (activity.Parameters.MasteryScore.HasValue) | |
registrationSubItem.AddOrUpdateParameter("adlcp:masteryscore", activity.Parameters.MasteryScore.ToString()); | |
if (activity.Parameters.MaxTimeAllowed.IsNotEmpty()) | |
registrationSubItem.AddOrUpdateParameter("adlcp:maxtimeallowed", activity.Parameters.MaxTimeAllowed); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RegistrationItem : Entity<RegistrationItem>, ILoggable | |
{ | |
#region Private Members | |
private ICollection<RegistrationSubItem> _subItems = new Collection<RegistrationSubItem>(); | |
private RegistrationStatus CurrentStatus { get; set; } | |
#endregion | |
#region Properties | |
public virtual ICollection<RegistrationSubItem> SubItems => _subItems; | |
public Registration Registration { get; protected set; } | |
public Content Content { get; protected set; } | |
public Class Class { get; protected set; } | |
public DateTime? FinishedAt { get; protected set; } | |
public RegistrationItemState State | |
{ | |
get { return RegistrationItemState.FromValue(CurrentStatus); } | |
protected set { CurrentStatus = value.Status; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public RegistrationItemMap() | |
{ | |
ToTable("RegistrationItems"); | |
HasKey(_ => _.Id); | |
HasRequired(_ => _.Registration).WithMany(_ => _.Items).Map(_ => _.MapKey("RegistrationId")); | |
HasRequired(_ => _.Content).WithMany().Map(_ => _.MapKey("ContentId")); | |
HasRequired(_ => _.Class).WithMany().Map(_ => _.MapKey("ClassId")); | |
HasMany(_ => _.SubItems).WithRequired(_ => _.RegistrationItem); | |
Property(_ => _.FinishedAt).IsOptional(); | |
Ignore(_ => _.State); | |
Ignore(_ => _.EntityName); | |
Ignore(_ => _.ModuleName); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RegistrationMap : EntityMap<Registration> | |
{ | |
public RegistrationMap() | |
{ | |
ToTable("Registrations"); | |
HasKey(_ => _.Id); | |
HasRequired(_ => _.Student).WithMany().Map(_ => _.MapKey("UserId")); | |
HasRequired(_ => _.Group).WithMany().Map(_ => _.MapKey("GroupId")); | |
Property(_ => _.RegisteredAt).IsRequired(); | |
Property(_ => _.FinishedAt).IsOptional(); | |
HasMany(_ => _.Items).WithRequired(_ => _.Registration); | |
Property(_ => _.IsFrozen); | |
Ignore(_ => _.State); | |
Ignore(_ => _.EntityName); | |
Ignore(_ => _.ModuleName); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RegistrationSubItem : Entity<RegistrationSubItem>, ILoggable | |
{ | |
#region Private Members | |
ICollection<RegistrationSubItemParameter> _parameters = new List<RegistrationSubItemParameter>(); | |
#endregion | |
#region Properties | |
public RegistrationItem RegistrationItem { get; protected set; } | |
public int ItemId { get; protected set; } | |
public RegistrationStatus Status { get; protected set; } | |
public int? Duration { get; protected set; } | |
public virtual ICollection<RegistrationSubItemParameter> Parameters => _parameters; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public RegistrationSubItemMap() | |
{ | |
ToTable("RegistrationSubItems"); | |
HasKey(_ => _.Id); | |
HasRequired(_ => _.RegistrationItem).WithMany(_ => _.SubItems).Map(_ => _.MapKey("RegistrationItemId")); | |
Property(_ => _.Duration).IsOptional(); | |
Property(_ => _.ItemId).IsRequired(); | |
Property(_ => _.Status).IsRequired(); | |
HasMany(_ => _.Parameters).WithRequired(_ => _.RegistrationSubItem); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public RegistrationSubItemParameterMap() | |
{ | |
ToTable("RegistrationSubItemParameters"); | |
Property(_ => _.Name).IsRequired().HasMaxLength(120); | |
Property(_ => _.Value).IsRequired().HasMaxLength(255); | |
HasRequired(_ => _.RegistrationSubItem).WithMany(_ => _.Parameters).Map(_ => _.MapKey("RegistrationSubItemId")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment