Ejemplo n.º 1
0
        public async Task ProcessDisarmedTrap(string userId, string disarmedTrapId)
        {
            UserResult user = await AppBusiness.User.GetById(userId);

            ArmedTrapResult disarmedTrap = await AppRepository.Trap.GetArmedTrapById(disarmedTrapId);

            UserResult userInjured   = user;
            UserResult userOwnerTrap = await AppBusiness.User.GetById(disarmedTrap.UserId);

            if (userOwnerTrap != null)
            {
                TrapResult trap = await AppBusiness.Trap.GetByNameKey(disarmedTrap.NameKey);

                UserRequest userOwnerRequest   = Mapper.Map <UserRequest>(userOwnerTrap);
                UserRequest userInjuredRequest = Mapper.Map <UserRequest>(userInjured);

                await AppBusiness.User.AddPoints(userOwnerRequest, trap.Points);

                /* Deixar de forma dinamica futuramente */
                var trapOwned = await AppBusiness.Trap.GetByNameKey("BEAR_TRAP");

                var trapUserOwned = new UserTrapRequest();

                trapUserOwned.TrapId = trapOwned.Id.ToString();
                trapUserOwned.Amount = 5;

                AppBusiness.User.AddTrap(userOwnerRequest, trapUserOwned);

                await AppBusiness.User.Save(userOwnerRequest);

                await SendNotificationToUserInjured(userInjuredRequest, userOwnerRequest, disarmedTrap);

                await SendNotificationToUserOwnerTrap(userInjuredRequest, userOwnerRequest, disarmedTrap, trap.Points);
            }
        }
        public async Task <List <ArmedTrapResult> > ListArmedTraps(float latitude, float longitude, string userId)
        {
            List <ArmedTrapResult> response = new List <ArmedTrapResult>();

            DbGeography searchLocation = DbGeography.FromText(string.Format("POINT({0} {1})", longitude.ToString(CultureInfo.InvariantCulture), latitude.ToString(CultureInfo.InvariantCulture)));

            var armedTraps = await
                                 (from armedTrap in AppRepository.EntitiesContext.ArmedTraps
                                 where
                                 !armedTrap.Disarmed &&
                                 armedTrap.User.Id != new Guid(userId)
                                 select new
            {
                Id        = armedTrap.Id,
                Date      = armedTrap.Date,
                UserId    = armedTrap.User.Id,
                Latitude  = armedTrap.Latitude,
                Longitude = armedTrap.Longitude,
                NameKey   = armedTrap.NameKey,
                Distance  = searchLocation.Distance(
                    DbGeography.FromText("POINT(" + armedTrap.Longitude + " " + armedTrap.Latitude + ")"))
            })
                             .OrderBy(a => a.Distance)
                             .Where(a => a.Distance < 500)
                             .ToListAsync();

            if (armedTraps != null)
            {
                foreach (var armedTrap in armedTraps)
                {
                    ArmedTrapResult armedTrapResponse = new ArmedTrapResult();

                    armedTrapResponse.Date      = armedTrap.Date;
                    armedTrapResponse.Id        = armedTrap.Id.ToString();
                    armedTrapResponse.Latitude  = armedTrap.Latitude;
                    armedTrapResponse.Longitude = armedTrap.Longitude;
                    armedTrapResponse.NameKey   = armedTrap.NameKey;
                    armedTrapResponse.UserId    = armedTrap.UserId.ToString();

                    response.Add(armedTrapResponse);
                }
            }

            return(response);
        }
        private ArmedTrapResult Parse(ArmedTrap armedTrap)
        {
            if (armedTrap != null)
            {
                ArmedTrapResult response = new ArmedTrapResult();

                response.Date      = armedTrap.Date;
                response.Disarmed  = armedTrap.Disarmed;
                response.Id        = armedTrap.Id.ToString();
                response.Latitude  = armedTrap.Latitude;
                response.Longitude = armedTrap.Longitude;
                response.NameKey   = armedTrap.NameKey;
                response.UserId    = armedTrap.User.Id.ToString();
                return(response);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
        private async Task SendNotificationToUserInjured(UserRequest userInjured, UserRequest userOwnerTrap, ArmedTrapResult disarmedTrap)
        {
            var message = StringUtils.GetStringForCulture("you_were_caught_trap", userInjured.Language);

            message = string.Format(message, userOwnerTrap.Name);

            string otherUserImage = string.Empty;

            if (userOwnerTrap.ProfilePicture != null && !string.IsNullOrEmpty(userOwnerTrap.ProfilePicture.Url))
            {
                otherUserImage = userOwnerTrap.ProfilePicture.Url;
            }

            await new NotificationBusiness().SendNotificationTrapDisarmed(userInjured.Email, message, false, 0, disarmedTrap.NameKey, disarmedTrap.Latitude, disarmedTrap.Longitude, userOwnerTrap.Name, otherUserImage);
        }