示例#1
0
        public async Task <OperationResult <IEnumerable <Course> > > GetCoursesForAuthor(Guid authorId)
        {
            using (CourseLibraryContext context = new CourseLibraryContext(options))
            {
                try
                {
                    var author = await AuthorCompiledQueries.GetAuthor(context, authorId);

                    if (author == null)
                    {
                        return(new FailedOperationResult <IEnumerable <Course> >
                        {
                            Code = ConstOperationCodes.AUTHOR_NOT_FOUND,
                        });
                    }
                    var result = await context.Courses.Where(c => c.AuthorId == authorId).ToListAsync();

                    return(new SuccessOperationResult <IEnumerable <Course> >
                    {
                        Result = result,
                        Code = ConstOperationCodes.SUCCESS_OPERATION
                    });
                }
                catch (Exception e)
                {
                    logger.Error($"error in course library get courses for author {e.Message}");
                    return(new FailedOperationResult <IEnumerable <Course> >
                    {
                        Code = ConstOperationCodes.FAILED_OPERATION
                    });
                }
            }
        }
示例#2
0
 public async Task <OperationResult <Author> > GetAuthor(Guid id, bool withCourses = false)
 {
     using (CourseLibraryContext context = new CourseLibraryContext(options))
     {
         try
         {
             Author author;
             if (withCourses)
             {
                 author = await AuthorCompiledQueries.GetAuthorWithCourses(context, id);
             }
             else
             {
                 author = await AuthorCompiledQueries.GetAuthor(context, id);
             }
             if (author != null)
             {
                 return(new SuccessOperationResult <Author>
                 {
                     Code = ConstOperationCodes.AUTHOR_FOUND,
                     Result = author
                 });
             }
             else
             {
                 return(new FailedOperationResult <Author>
                 {
                     Code = ConstOperationCodes.AUTHOR_NOT_FOUND
                 });
             }
         }
         catch (Exception e)
         {
             logger.Error($"error in course library context getting author {e.Message}");
             return(new FailedOperationResult <Author>
             {
                 Code = ConstOperationCodes.FAILED_OPERATION
             });
         }
     }
 }