//[Test]
        //public void OutputForms_Get()
        //{
        //    var dbContext = TypiconDbContextFactory.Create();
        //    var outputForms = OutputFormsFactory.Create(dbContext);

        //    DeleteAllOutputDays(dbContext, TYPICON_ID);

        //    Result<LocalizedOutputDay> task = outputForms.GetDay(TYPICON_ID, new DateTime(2020, 1, 1), "cs-ru");

        //    Assert.IsTrue(task.Failure);
        //}

        private void DeleteAllOutputDays(TypiconDBContext dbContext, int typiconId)
        {
            var forms = dbContext.Set <OutputDay>().Where(c => c.TypiconId == typiconId);

            dbContext.Set <OutputDay>().RemoveRange(forms);

            dbContext.SaveChanges();
        }
        public static void UpdateOutputDay(this TypiconDBContext dbContext, OutputDay outputDay)
        {
            var outputDays = dbContext.Set <OutputDay>()
                             .Where(c => c.TypiconId == outputDay.TypiconId && c.Date.Date == outputDay.Date.Date);

            if (outputDays.Any())
            {
                dbContext.Set <OutputDay>().RemoveRange(outputDays);
            }

            dbContext.Set <OutputDay>().Add(outputDay);

            dbContext.SaveChanges();
        }
        public async Task <Result> ExecuteAsync(TCommand command)
        {
            var result = await _decoratee.ExecuteAsync(command);

            if (result.Success)
            {
                _dbContext.ChangeTracker
                .Entries()
                .Where(c => c.Entity is IHasDomainEvents)
                .SelectMany(c => (c.Entity as IHasDomainEvents).GetDomainEvents())
                .ToList()
                .ForEach(c => _eventDispatcher.Dispatch(c));

                _dbContext.SaveChanges();
            }

            return(Result.Ok());
        }
Esempio n. 4
0
        public Result Import(byte[] file)
        {
            try
            {
                var xml = Encoding.UTF8.GetString(file);

                var projection = _serializer.Deserialize <TypiconVersionProjection>(xml);

                var entity = _projector.Project(projection);

                _dbContext.Set <TypiconEntity>().Add(entity.Value);

                int i = _dbContext.SaveChanges();

                return(Result.Ok());
            }
            catch (Exception ex)
            {
                return(Result.Fail(ex.Message));
            }
        }
Esempio n. 5
0
 public void SaveChanges()
 {
     dbContext.SaveChanges();
 }
        public static void UpdateModifiedYear(this TypiconDBContext dbContext, ModifiedYear modifiedYear)
        {
            dbContext.Set <ModifiedYear>().Update(modifiedYear);

            dbContext.SaveChanges();
        }
Esempio n. 7
0
        protected override async Task <Result> DoTheJob(PublishTypiconJob job)
        {
            //находим черновик
            var found = _dbContext.GetTypiconVersion(job.TypiconId, TypiconVersionStatus.Draft);

            if (found.Failure)
            {
                //TODO: SendMessage to Owner and sender

                return(Fail(job, found.Error));
            }

            var version = found.Value;

            var errMsg = version.Validate();

            if (errMsg.Count() > 0)
            {
                return(Fail(job, string.Join("", "Ошибка: ", errMsg)));
            }

            var prevStatus = version.Typicon.Status;

            //TypiconEntity
            version.Typicon.Status = TypiconStatus.Publishing;
            await _dbContext.UpdateTypiconEntityAsync(version.Typicon);

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    version.IsModified = false;
                    version.ModifiedYears.Clear();

                    //new draft
                    var clone = version.Clone();
                    //задаем доп значения
                    clone.TypiconId     = job.TypiconId;
                    clone.VersionNumber = version.VersionNumber + 1;
                    clone.BDate         = null;
                    //Сохраняем, чтобы не было конфликта цикличных зависимостей
                    await _dbContext.UpdateTypiconVersionAsync(clone);

                    //копируем вложенные коллекции
                    version.CopyChildrenTo(clone);
                    //сохраняем
                    await _dbContext.UpdateTypiconVersionAsync(clone);

                    //old publish
                    var oldPublish = _dbContext.GetTypiconVersion(job.TypiconId, TypiconVersionStatus.Published);
                    if (oldPublish.Success)
                    {
                        oldPublish.Value.EDate = DateTime.Now;
                    }

                    //new publish
                    version.BDate = DateTime.Now;

                    //outputforms
                    await _dbContext.ClearOutputFormsAsync(job.TypiconId, job.DeleteModifiedOutputDays);

                    //TODO: SendMessage to Owner and sender


                    //typiconEntity
                    version.Typicon.Status = TypiconStatus.Published;

                    _dbContext.SaveChanges();

                    transaction.Commit();

                    return(Finish(job));
                }
                catch (Exception ex)
                {
                    transaction.Rollback();

                    //version.Typicon.Status = prevStatus;
                    //version.IsModified = true;
                    await _dbContext.UpdateTypiconEntityStatusAsync(version.TypiconId, prevStatus);

                    return(Fail(job, ex.Message));
                }
            }
        }