Ejemplo n.º 1
0
        public NoteCatalog Update(NoteCatalog entity)
        {
            Guard.Against <ArgumentNullException>(entity == null, nameof(entity));

            // ReSharper disable once PossibleNullReferenceException
            if (entity.Id <= 0)
            {
                ProcessMessage.Success = false;
                ProcessMessage.AddErrorMessage($"Can not update NoteCatalog with id {entity.Id}", true);
                return(null);
            }

            try
            {
                // check if need apply default render
                var          render  = PropertyChecking(entity.Render);
                const string message = "Cannot find default note render.";
                ProcessMessage.Success = false;
                ProcessMessage.AddErrorMessage(message, true);
                entity.Render = render ?? throw new DataSourceException(message);

                DataContext.Catalogs.Update(entity);
                DataContext.Save();
                return(LookupRepo.GetEntity <NoteCatalog>(entity.Id));
            }
            catch (Exception ex)
            {
                ProcessMessage.WrapException(ex);
                return(null);
            }
        }
Ejemplo n.º 2
0
        public NoteRender Update(NoteRender entity)
        {
            Guard.Against <ArgumentNullException>(entity == null, nameof(entity));

            // ReSharper disable once PossibleNullReferenceException
            if (entity.Id <= 0)
            {
                ProcessMessage.Success = false;
                ProcessMessage.AddErrorMessage($"Can not update NoteRender with id {entity.Id}", true);
                return(null);
            }

            try
            {
                // make sure the record exists in data source
                DataContext.Renders.Update(entity);
                DataContext.Save();
                return(LookupRepo.GetEntity <NoteRender>(entity.Id));
            }
            catch (Exception ex)
            {
                ProcessMessage.WrapException(ex);
                return(null);
            }
        }
Ejemplo n.º 3
0
        protected TP PropertyChecking <TP>(TP property) where TP : HasDefaultEntity
        {
            var defaultNeeded = false;

            if (property == null)
            {
                defaultNeeded = true;
            }
            else if (property.Id <= 0)
            {
                defaultNeeded = true;
            }
            else if (LookupRepo.GetEntity <TP>(property.Id) == null)
            {
                defaultNeeded = true;
            }

            if (!defaultNeeded)
            {
                return(property);
            }

            var defaultProp = LookupRepo.GetEntities <TP>(p => p.IsDefault).FirstOrDefault();

            return(defaultProp);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get notes for specific entity
        /// </summary>
        /// <param name="entity">The entity which used to get type and figure out the catalog</param>
        /// <param name="query">Query for note</param>
        /// <param name="resourceCollectionParameters">The page information of the resource collection</param>
        /// <returns>The notes which belongs to entity type</returns>
        protected PageList <HmmNote> GetNotes(T entity, Expression <Func <HmmNote, bool> > query = null, ResourceCollectionParameters resourceCollectionParameters = null)
        {
            var catId = entity.GetCatalogId(LookupRepo);

            var hasValidAuthor = DefaultAuthor != null && LookupRepo.GetEntity <Author>(DefaultAuthor.Id) != null;

            switch (hasValidAuthor)
            {
            case false:
                ProcessResult.AddErrorMessage("Cannot find default author", true);
                return(null);

            default:
                PageList <HmmNote> notes;
                if (query != null)
                {
                    var finalExp = query.And(n => n.Author.Id == DefaultAuthor.Id && n.Catalog.Id == catId);
                    notes = NoteManager.GetNotes(finalExp, false, resourceCollectionParameters);
                }
                else
                {
                    notes = NoteManager.GetNotes(n => n.Author.Id == DefaultAuthor.Id && n.Catalog.Id == catId, false, resourceCollectionParameters);
                }

                return(notes);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get notes for specific entity
        /// </summary>
        /// <param name="id">The id of entity to get type and figure out the catalog</param>
        /// <param name="entity">The entity by id which used to get type and figure out the catalog</param>
        /// <returns>The notes which belongs to entity type</returns>
        protected async Task <HmmNote> GetNoteAsync(int id, T entity)
        {
            var catId = await entity.GetCatalogIdAsync(LookupRepo);

            var hasValidAuthor = DefaultAuthor != null && LookupRepo.GetEntity <Author>(DefaultAuthor.Id) != null;

            switch (hasValidAuthor)
            {
            case false:
                ProcessResult.AddErrorMessage("Cannot find default author", true);
                return(null);

            default:

                var note = await NoteManager.GetNoteByIdAsync(id);

                if (note == null)
                {
                    return(null);
                }

                if (note.Author.Id == DefaultAuthor.Id && note.Catalog.Id == catId)
                {
                    return(note);
                }

                return(null);
            }
        }
Ejemplo n.º 6
0
        public async Task <Subsystem> UpdateAsync(Subsystem entity)
        {
            Guard.Against <ArgumentNullException>(entity == null, nameof(entity));

            // ReSharper disable once PossibleNullReferenceException
            if (entity.Id <= 0)
            {
                ProcessMessage.Success = false;
                ProcessMessage.AddErrorMessage($"Can not update Subsystem with id {entity.Id}", true);
                return(null);
            }

            try
            {
                // make sure the record exists in data source
                DataContext.Subsystems.Update(entity);
                await DataContext.SaveAsync();

                return(LookupRepo.GetEntity <Subsystem>(entity.Id));
            }
            catch (Exception ex)
            {
                ProcessMessage.WrapException(ex);
                return(null);
            }
        }
Ejemplo n.º 7
0
        public async Task <HmmNote> UpdateAsync(HmmNote entity)
        {
            Guard.Against <ArgumentNullException>(entity == null, nameof(entity));

            try
            {
                // check if need apply default catalog
                // ReSharper disable once PossibleNullReferenceException
                var catalog = PropertyChecking(entity.Catalog);
                entity.Catalog = catalog ?? throw new Exception("Cannot find default note catalog.");

                entity.LastModifiedDate = DateTimeProvider.UtcNow;
                DataContext.Notes.Update(entity);
                await DataContext.SaveAsync();

                var savedRec = LookupRepo.GetEntity <HmmNote>(entity.Id);

                return(savedRec);
            }
            catch (Exception ex)
            {
                ProcessMessage.WrapException(ex);
                return(null);
            }
        }
Ejemplo n.º 8
0
        private bool IsAuthorValid(User author)
        {
            // make sure note author is exists
            if (author == null || author.Id <= 0)
            {
                ValidationErrors.Add("Error: invalid author attached to note.");
                return(false);
            }

            var savedAuthor = LookupRepo.GetEntity <User>(author.Id);

            if (savedAuthor == null)
            {
                ValidationErrors.Add("Error: cannot find author from data source.");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 9
0
        public override bool IsValid(HmmNote entity, bool isNewEntity)
        {
            if (entity == null)
            {
                ValidationErrors.Add("Error: null note found.");
                return(false);
            }

            // make sure note author is exists
            if (isNewEntity)
            {
                if (!IsAuthorValid(entity.Author))
                {
                    return(false);
                }
            }

            if (!isNewEntity)
            {
                if (entity.Id <= 0)
                {
                    ValidationErrors.Add($"The note get invalid id {entity.Id}");
                    return(false);
                }

                var rec = LookupRepo.GetEntity <HmmNote>(entity.Id);
                if (rec == null)
                {
                    ValidationErrors.Add($"Cannot find note with id {entity.Id} from data source");
                    return(false);
                }

                // check if user want to update author for note which does not allowed by system
                var newAuthorId = entity.Author.Id;
                if (rec.Author.Id != newAuthorId)
                {
                    ValidationErrors.Add($"Cannot update note: {entity.Id}'s author. current author id: {rec.Id}, new author id: {newAuthorId}");
                    return(false);
                }
            }

            return(true);
        }