Esempio n. 1
0
        public ReadAllActivityForResearcherHandlerTests()
        {
            _unitOfWork         = new Mock <IUnitOfWork>();
            _activityRepository = new Mock <IActivityRepository <Activity> >();

            var activity1 = new ActivityResearcher()
            {
                ActivityId   = 1,
                ResearcherId = 1,
                UserId       = 1,
                Created      = DateTime.Now,
                Id           = 1
            };
            var activity2 = new ActivityResearcher()
            {
                ActivityId   = 3,
                ResearcherId = 1,
                UserId       = 1,
                Created      = DateTime.Now,
                Id           = 1
            };
            var activity3 = new ActivityResearcher()
            {
                ActivityId   = 2,
                ResearcherId = 1,
                UserId       = 1,
                Created      = DateTime.Now,
                Id           = 1
            };
            var activityList = new List <ActivityWithUserModel>();
            ActivityWithUserModel activityWithUserModel = new ActivityWithUserModel()
            {
                ActivityName = "Tester1",
                Username     = "******",
                Id           = 1
            };
            ActivityWithUserModel activityWithUserModel1 = new ActivityWithUserModel()
            {
                ActivityName = "Tester2",
                Username     = "******",
                Id           = 2
            };
            ActivityWithUserModel activityWithUserModel2 = new ActivityWithUserModel()
            {
                ActivityName = "Tester3",
                Username     = "******",
                Id           = 3
            };

            listActivityWithUserModels = new List <ActivityWithUserModel>();
            listActivityWithUserModels.Add(activityWithUserModel1);
            listActivityWithUserModels.Add(activityWithUserModel2);
            listActivityWithUserModels.Add(activityWithUserModel);
        }
        public async Task <List <ActivityWithUserModel> > ReadAllActivityForResearcher(int id)
        {
            try
            {
                string readAllActivityForResearcherQuery = "SELECT [Activity].ActivityName, ActivityResearcher.Id, " +
                                                           "ActivityResearcher.Created, [User].Id, [User].Username " +
                                                           "FROM [EIC].[dbo].[Activity] " +
                                                           "left JOIN ActivityResearcher on Activity.Id = ActivityResearcher.ActivityId " +
                                                           "left JOIN Researcher on ActivityResearcher.ResearcherId = Activity.Id " +
                                                           "left JOIN [User] on ActivityResearcher.UserId = [User].Id " +
                                                           "WHERE ActivityResearcher.ResearcherId = " + id + ";";

                var results = new List <ActivityWithUserModel>();
                var result  = await this._connection.QueryAsync <Activity, ActivityResearcher, User, Activity>(readAllActivityForResearcherQuery, (activity, activityResearcher, user) =>
                {
                    var model = results.FirstOrDefault(c => c.Id == activityResearcher.Id);
                    if (model == null)
                    {
                        var activityWithUserModel     = new ActivityWithUserModel();
                        activityWithUserModel.Id      = activityResearcher.Id;
                        activityWithUserModel.Created = activityResearcher.Created;
                        results.Add(activityWithUserModel);
                        model = activityWithUserModel;
                    }

                    if (model.ActivityName == null && activity != null)
                    {
                        model.ActivityName = activity.ActivityName;
                    }
                    if (model.Username == null && user != null)
                    {
                        model.Username = user.UserName;
                    }

                    return(null);
                }, transaction : this._transaction);

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

                return(results);
            }
            catch (Exception e)
            {
                return(null);
            }
        }