Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] VolunteerType volunteerType)
        {
            if (id != volunteerType.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(volunteerType);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!VolunteerTypeExists(volunteerType.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(volunteerType));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutVolunteerType([FromRoute] int id, [FromBody] VolunteerType volunteerType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != volunteerType.Id)
            {
                return(BadRequest());
            }

            _context.Entry(volunteerType).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VolunteerTypeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        //abstract class constructors for propertyManager below
        //======================================================================================

        //Updates the value of this property, only set fromSaveFile flag to true when called when called after loading it from a file
        public void UpdateReward(int tWorked, VolunteerType nWork, bool fromSaveFile = false)
        {
            this.timesWorked = tWorked;
            this.nameOfWork  = nWork;

            if (!fromSaveFile)
            {
                SaveToFile();
            }
        }
        public async Task GetAllStreetChampions()
        {
            VolunteerCache volunteerCache = new VolunteerCache(_memDistCache.Object, _volunteersForCacheGetter.Object);

            VolunteerType volunteerType             = VolunteerType.StreetChampion;
            IEnumerable <CachedVolunteerDto> result = await volunteerCache.GetCachedVolunteersAsync(volunteerType, CancellationToken.None);

            Assert.AreEqual(2, result.Count());
            Assert.IsTrue(result.Any(x => x.UserId == 3));
            Assert.IsTrue(result.Any(x => x.UserId == 4));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("Id,Name")] VolunteerType volunteerType)
        {
            if (ModelState.IsValid)
            {
                _context.Add(volunteerType);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(volunteerType));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> PostVolunteerType([FromBody] VolunteerType volunteerType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.VolunteerTypes.Add(volunteerType);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetVolunteerType", new { id = volunteerType.Id }, volunteerType));
        }
        public async Task GetAllHelpers()
        {
            VolunteerCache volunteerCache = new VolunteerCache(_memDistCache.Object, _volunteersForCacheGetter.Object);

            VolunteerType  volunteerType            = VolunteerType.Helper;
            IsVerifiedType isVerifiedType           = IsVerifiedType.IsVerified | IsVerifiedType.IsNotVerified;
            IEnumerable <CachedVolunteerDto> result = await volunteerCache.GetCachedVolunteersAsync(volunteerType, CancellationToken.None);

            Assert.AreEqual(2, result.Count());
            Assert.IsTrue(result.Any(x => x.UserId == 1));
            Assert.IsTrue(result.Any(x => x.UserId == 2));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            VolunteerType = await _context.VolunteerType.FirstOrDefaultAsync(m => m.VolunteerTypeID == id);

            if (VolunteerType == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Ejemplo n.º 9
0
        public async Task <IEnumerable <HelperWithinRadiusDTO> > GetHelpersWithinRadius(string postcode, double?overrideVolunteerRadius, CancellationToken token)
        {
            var helpers = new List <HelperWithinRadiusDTO>();
            LatitudeAndLongitudeDTO comparePostcode = _repository.GetLatitudeAndLongitude(postcode);

            VolunteerType volunteerType = VolunteerType.Helper | VolunteerType.StreetChampion;
            Task <IEnumerable <CachedVolunteerDto> > cachedVolunteerDtosTask = _volunteerCache.GetCachedVolunteersAsync(volunteerType, token);

            await Task.WhenAll(cachedVolunteerDtosTask);

            IEnumerable <CachedVolunteerDto> cachedVolunteerDtos = await cachedVolunteerDtosTask;

            Dictionary <int, double> idsOfHelpersWithinRadius = new Dictionary <int, double>();

            foreach (CachedVolunteerDto cachedVolunteerDto in cachedVolunteerDtos)
            {
                double distance = _distanceCalculator.GetDistanceInMiles(comparePostcode.Latitude, comparePostcode.Longitude, cachedVolunteerDto.Latitude, cachedVolunteerDto.Longitude);

                bool isWithinSupportRadius = distance <= (overrideVolunteerRadius.HasValue? overrideVolunteerRadius.Value : cachedVolunteerDto.SupportRadiusMiles);

                if (isWithinSupportRadius)
                {
                    idsOfHelpersWithinRadius.Add(cachedVolunteerDto.UserId, distance);
                }
            }
            var users = await _repository.GetVolunteersByIdsAsync(idsOfHelpersWithinRadius.Keys);



            if (users.Any())
            {
                helpers = users.Select(x => new HelperWithinRadiusDTO {
                    User = x, Distance = idsOfHelpersWithinRadius[x.ID]
                }).ToList();
            }
            return(helpers);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Get volunteers using cache.
        /// </summary>
        public async Task <IEnumerable <CachedVolunteerDto> > GetCachedVolunteersAsync(VolunteerType volunteerType, CancellationToken cancellationToken)
        {
            // Don't refresh data in cache because this will be refreshed through a Timed trigger.  This is because getting dependencies on a new thread using IServiceScopeFactory throws an error in Azure Functions (Scope disposed{no name, Parent={no name}} is disposed and scoped instances are disposed and no longer available).
            IEnumerable <CachedVolunteerDto> cachedVolunteerDtos = await _memDistCache.GetCachedDataAsync(async (token) => await _volunteersForCacheGetter.GetAllVolunteersAsync(token), CacheKey.AllCachedVolunteerDtos.ToString(), RefreshBehaviour.DontRefreshData, cancellationToken);

            List <CachedVolunteerDto> matchingVolunteers = cachedVolunteerDtos.Where(x => x.VolunteerType.HasAnyFlags(volunteerType)).ToList();

            return(matchingVolunteers);
        }
Ejemplo n.º 11
0
 //Resets the value of this property to an empty List
 public override void Restore()
 {
     timesWorked = 0;
     nameOfWork  = VolunteerType.None;
     SaveToFile();
 }
Ejemplo n.º 12
0
 /// <summary> Method to set the type of volunteer work player has chosen. </summary>
 public void SetVolunteerName(VolunteerType type)
 {
     nameOfWork = type;
     SaveToFile();
 }