Exemple #1
0
        public override ICharity GetCharityById(int id)
        {
            try
            {
                Charity charity = _store.Charities
                                  .Include(e => e.Category)
                                  .Include(e => e.CreatedBy)
                                  .Include(e => e.UpdatedBy)
                                  .Include(e => e.Locations)
                                  .Include(e => e.Volunteers)
                                  .Single(e => e.Id == id);

                charity.TotalHours = Math.Abs((_store.Hours
                                               .Where(x => x.CharityId == id)
                                               .Sum(x => DbFunctions.DiffMinutes(x.EndTime, x.StartTime)) ?? 0) / 60);

                charity.FollowersCount = charity.Volunteers.Count();

                charity.AverageRating = _store.Hours
                                        .Where(x => x.CharityId == id)
                                        .Average(g =>
                                                 g.CharityRating
                                                 );

                return(CharityFactory.CreateCharity(charity));
            }
            catch (InvalidOperationException)
            {
                return(null);
            }
        }
Exemple #2
0
        public override int CreateCharity(ICharity charityDTO)
        {
            if (_store.Charities.Any(x => x.Name == charityDTO.Name))
            {
                throw new ArgumentException(); //To-Do: Make custom exception for when duplicate names are found
            }
            Charity c = CharityFactory.CreateCharity(charityDTO);

            if (charityDTO.Category != null)
            {
                c.CategoryId = charityDTO.Category.Id;
            }
            if (charityDTO.CreatedBy != null)
            {
                c.CreatedById = charityDTO.CreatedBy.Id;
            }

            if (charityDTO.UpdatedBy != null)
            {
                c.UpdatedById = charityDTO.UpdatedBy.Id;
            }
            c = _store.Charities.Add(c);
            _store.SaveChanges();
            return(c.Id);
        }
Exemple #3
0
        public override IEnumerable <ICharity> GetAllCharities()
        {
            IEnumerable <Charity> c = _store.Charities
                                      .Include(e => e.Category)
                                      .Include(e => e.CreatedBy)
                                      .Include(e => e.UpdatedBy)
                                      .Include(e => e.Locations)
                                      .ToList();

            return(c.Select(e => CharityFactory.CreateCharity(e)));
        }
Exemple #4
0
        public int CreateHour(IHour hourDTO)
        {
            Hour h = HourFactory.CreateHour(hourDTO);

            Charity c = _store.Charities.SingleOrDefault(e => e.Id == hourDTO.Charity.Id);

            if (c == null && hourDTO.Charity != null)
            {
                _store.Charities.Add(CharityFactory.CreateCharity(hourDTO.Charity));
            }

            h.VolunteerId = hourDTO.Volunteer.Id;
            h.CharityId   = hourDTO.Charity.Id;

            _store.Hours.Add(h);
            _store.SaveChanges();

            return(h.Id);
        }