// CONSTRUCTORS /// <summary> /// Initializes a new instance of <see cref="SingleViewModel"/> /// </summary> /// <param name="subject"> /// An instance of <see cref="DataAccess.Entities.Subject"/> /// </param> /// <param name="isNew"> /// Determines is this entities is new /// </param> /// <param name="isEditingEnabled"> /// Determines is editing allowed /// </param> public SingleViewModel(DataAccess.Entities.Subject subject, bool isNew, bool isEditingEnabled = true) : base(shownEntity: subject, isWritingEnabled: isEditingEnabled) { this.isNew = isNew; insertUpdateCommand = new Commands.Admin.Subject.Single.CreateUpdateCommand(this); Logger.LogAsync(Core.LogMode.Debug, $"{nameof(SingleViewModel)} created"); }
/// <summary> /// Executes the command /// </summary> /// <param name="parameter"> /// Command parameter /// <para/> /// True if value should be created, false if updated /// </param> public override void Execute(object parameter) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Execute {nameof(CreateUpdateCommand)}"); // gets entity DataAccess.Entities.Subject subject = (DataAccess.Entities.Subject)subjectSingleViewModel.ShownEntity; // get new subject name string subjectName = subject.Name; Core.Logger.GetLogger.LogAsync(Core.LogMode.Info, $"New subject name = {subjectName}"); // check if right if (subjectName.Length > Core.Configuration.DBConfig.ADMIN_MESSAGE_SUBJECT_MAX_LENGTH || subjectName.Length < Core.Configuration.DBConfig.ADMIN_MESSAGE_SUBJECT_MIN_LENGTH) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Subject name is wrong. Interrupt command executing"); Services.WindowManager.Instance.ShowMessageWindow(Core.Messages.Info.Admin.ADMIN_WRONG_SUBJECT_LENGTH); return; } // create or update, depend on parameter bool isNew = System.Convert.ToBoolean(parameter); if (isNew) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Create new subject"); DataAccess.Context.UnitOfWork.Instance .SubjectRepository .Insert(subject); } else { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update subject"); DataAccess.Context.UnitOfWork.Instance .SubjectRepository .Update(subject); } // save changes Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Save changes"); DataAccess.Context.UnitOfWork.Instance.Save(); // go back to all items Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Go to previous content"); Services.NavigationManager.Instance.NavigateToPrevious(parent: Services.DataStorage.Instance.AdminWindowContentControl); }