예제 #1
0
        public async Task <IActionResult> Start(ActivityActionViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(HttpContext.User);

                var userId = _userRepository.GetIdByIdentity(user.Id);

                var id       = model.Id;
                var activity = _activityRepository.Get(id);
                if (activity.Status == 0)
                {
                    if (Program.UseShSend)
                    {
                        _activityRepository.Start(id);
                    }
                    else if (Program.UseEventHub)
                    {
                        await _eventHub.SendMessage(new QueueElement <object>
                        {
                            Type = "ActivityStart",
                            Data = new ActivityIdViewModel()
                            {
                                Id = id, IdUser = userId
                            },
                        });
                    }
                    else
                    {
                        var queue = new ServiceBusManager(_configuration["ServiceBusConnectionString"],
                                                          _configuration["ServiceBusQueueName"]);
                        await queue.SendMessage(new QueueElement <object>
                        {
                            Type = "ActivityStart",
                            Data = new ActivityIdViewModel()
                            {
                                Id = id, IdUser = userId
                            },
                        });
                    }
                }
            }
            return(RedirectToAction("Index"));
        }
예제 #2
0
        public async Task <IActionResult> Close(ActivityActionViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(HttpContext.User);

                var userId = _userRepository.GetIdByIdentity(user.Id);

                var id = model.Id;
                await _eventHub.SendMessage(new QueueElement <object>
                {
                    Type = "ActivityClose",
                    Data = new ActivityIdViewModel()
                    {
                        Id = id, IdUser = userId
                    },
                });
            }
            return(RedirectToAction("Index"));
        }
예제 #3
0
        public async Task <object> Create([FromRoute] int id, [FromBody] TelemetrySendViewModel model)
        {
            var activityId = id;

            if (!ModelState.IsValid)
            {
                return(new { error = "Bad request." });
            }

            var user = await _userManager.GetUserAsync(HttpContext.User);

            var userData = _userRepository.GetUserByIdentity(user.Id);

            var userId = userData.Id;

            model.IdUser     = userId;
            model.IdActivity = activityId;

            // This should be changed in real code to prevent extra queries
            var activityInfo = _activityRepository.Get(model.IdActivity);
            var runnerAdded  = (activityInfo.Type == 1 || _activityRepository.IsRunnerAdded(model.IdActivity, userData.Id));

            if (userData.IsOrganizer || activityInfo.Status != 1 || !runnerAdded) // Not allowed or closed
            {
                return(new { error = "Unauthorized." });
            }

            // Known format of URL
            if (activityInfo.RaceUrl.Length > 0)
            {
                var newId = 0;
                int.TryParse(activityInfo.RaceUrl.Substring(activityInfo.RaceUrl.LastIndexOf("/") + 1), out newId);
                model.IdActivityOrganizer = newId;
            }

            await _eventHub.SendMessage(new QueueElement <TelemetrySendViewModel>
            {
                Type = "TelemetrySend",
                Data = model,
            });

            return(new
            {
                coords = new
                {
                    latitude = model.Latitude,
                    longitude = model.Longitude,
                },
                imageUrl = model.Image,
            });
        }
예제 #4
0
        public async Task <Activity> Join(int id, [FromBody] ActivityStringViewModel data)
        {
            // checks would go here. oh well
            await _eventHub.SendMessage(new QueueElement <ActivityIdViewModel>
            {
                Type = "ActivityJoin",
                Data = new ActivityIdViewModel()
                {
                    Id = id, IdUser = data.IdUser
                },
            });

            var activity = _activityRepository.Get(id);

            return(activity);
        }
예제 #5
0
        public async Task <IActionResult> EditProfile(UserPhotoUpdateFormViewModel model)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var userId = _userRepository.GetIdByIdentity(user.Id);

            // Placeholder permissions check
            if (model.IdUser != userId)
            {
                return(Unauthorized());
            }

            // Sanity checks
            var validUser = _userRepository.Get(model.IdUser);

            if (!ModelState.IsValid || validUser?.Id == null || validUser?.Id != userId)
            {
                return(RedirectToAction("Index"));
                //return BadRequest();
            }



            // Informazioni foto
            var fileExtension = Path.GetExtension(model.Image.FileName);
            // Sistema il content type
            var contentType = "";

            new FileExtensionContentTypeProvider().TryGetContentType(model.Image.FileName, out contentType);

            // Converti stream in base64
            var base64 = "";

            using (var stream = model.Image.OpenReadStream())
            {
                var outStream = new MemoryStream();
                stream.CopyTo(outStream);
                var bytes = outStream.ToArray();
                base64 = Convert.ToBase64String(bytes);
            }

            // Invia risultati

            if (Program.UseEventHub)
            {
                await _eventHub.SendMessage(new QueueElement <UserPhotoUpdateViewModel>
                {
                    Type = "UserPhotoUpdate",
                    Data = new UserPhotoUpdateViewModel()
                    {
                        ContentType = contentType,
                        IdUser      = model.IdUser,
                        Image       = base64,
                    },
                });
            }
            else
            {
                var queue = new ServiceBusManager(_configuration["ServiceBusConnectionString"], _configuration["ServiceBusQueueName"]);
                await queue.SendMessage(new QueueElement <UserPhotoUpdateViewModel>
                {
                    Type = "UserPhotoUpdate",
                    Data = new UserPhotoUpdateViewModel()
                    {
                        ContentType = contentType,
                        IdUser      = model.IdUser,
                        Image       = base64,
                    },
                });
            }



            return(NoContent());
            // await _client.PostRequest<object>($"User/{model.IdUser}/UpdatePhoto", );

            //return RedirectToAction("Index");
        }