Ejemplo n.º 1
0
        /// <summary>
        /// Creates a bookmark asyncronously
        /// </summary>
        /// <param name="newBookmark">The model to create</param>
        /// <returns>The created bookmark</returns>
        public async Task <Bookmark> CreateBookmarkAsync(NewBookmark newBookmark)
        {
            var modelHasOneResourceId = ModelHasOneResourceId(newBookmark);

            if (!modelHasOneResourceId)
            {
                throw new EcaBusinessException(MODEL_HAS_LESS_THAN_OR_MORE_THAN_ONE_RESOURCE_ID_ERROR);
            }

            var bookmarkAlreadyExists = BookmarkAlreadyExists(newBookmark);

            if (bookmarkAlreadyExists)
            {
                throw new EcaBusinessException(BOOKMARK_ALREADY_EXISTS_ERROR);
            }

            var resourceExists = await ResourceExists(newBookmark);

            if (!resourceExists)
            {
                throw new ModelNotFoundException(RESOURCE_DOES_NOT_EXIST_ERROR);
            }

            var bookmark = DoCreate(newBookmark);

            return(bookmark);
        }
Ejemplo n.º 2
0
        private async Task <bool> ResourceExists(NewBookmark newBookmark)
        {
            Object resource = null;

            if (newBookmark.OfficeId != null)
            {
                resource = await Context.Organizations.FindAsync(newBookmark.OfficeId);
            }
            else if (newBookmark.ProgramId != null)
            {
                resource = await Context.Programs.FindAsync(newBookmark.ProgramId);
            }
            else if (newBookmark.ProjectId != null)
            {
                resource = await Context.Projects.FindAsync(newBookmark.ProjectId);
            }
            else if (newBookmark.PersonId != null)
            {
                resource = await Context.People.FindAsync(newBookmark.PersonId);
            }
            else if (newBookmark.OrganizationId != null)
            {
                resource = await Context.Organizations.FindAsync(newBookmark.OrganizationId);
            }

            return(resource != null);
        }
Ejemplo n.º 3
0
        private bool BookmarkAlreadyExists(NewBookmark newBookmark)
        {
            var bookmark = Context.Bookmarks.Where(x => x.OfficeId == newBookmark.OfficeId &&
                                                   x.ProgramId == newBookmark.ProgramId &&
                                                   x.ProjectId == newBookmark.ProjectId &&
                                                   x.PersonId == newBookmark.PersonId &&
                                                   x.OrganizationId == newBookmark.OrganizationId &&
                                                   x.PrincipalId == newBookmark.PrincipalId).FirstOrDefault();

            return(bookmark != null);
        }
Ejemplo n.º 4
0
        private bool ModelHasOneResourceId(NewBookmark newBookmark)
        {
            var hasOneId = false;

            if ((newBookmark.OfficeId != null && newBookmark.ProgramId == null && newBookmark.ProjectId == null && newBookmark.PersonId == null && newBookmark.OrganizationId == null) ||
                (newBookmark.OfficeId == null && newBookmark.ProgramId != null && newBookmark.ProjectId == null && newBookmark.PersonId == null && newBookmark.OrganizationId == null) ||
                (newBookmark.OfficeId == null && newBookmark.ProgramId == null && newBookmark.ProjectId != null && newBookmark.PersonId == null && newBookmark.OrganizationId == null) ||
                (newBookmark.OfficeId == null && newBookmark.ProgramId == null && newBookmark.ProjectId == null && newBookmark.PersonId != null && newBookmark.OrganizationId == null) ||
                (newBookmark.OfficeId == null && newBookmark.ProgramId == null && newBookmark.ProjectId == null && newBookmark.PersonId == null && newBookmark.OrganizationId != null))
            {
                hasOneId = true;
            }

            return(hasOneId);
        }
Ejemplo n.º 5
0
        private Bookmark DoCreate(NewBookmark newBookmark)
        {
            var bookmark = new Bookmark
            {
                OfficeId       = newBookmark.OfficeId,
                ProgramId      = newBookmark.ProgramId,
                ProjectId      = newBookmark.ProjectId,
                PersonId       = newBookmark.PersonId,
                OrganizationId = newBookmark.OrganizationId,
                PrincipalId    = newBookmark.PrincipalId,
                AddedOn        = DateTimeOffset.Now,
                Automatic      = newBookmark.Automatic
            };

            this.Context.Bookmarks.Add(bookmark);

            return(bookmark);
        }