예제 #1
0
        public IActionResult CheckCurrentRegistered()
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Unauthorized("You need to be logged in to perform this action"));
            }

            string mailAdress = User.Identity.Name;

            if (mailAdress == null)
            {
                return(BadRequest("User not found"));
            }

            var user = _userRepository.GetBy(mailAdress);

            if (user == null)
            {
                return(BadRequest("User not found"));
            }

            var registration = _registrationRepository.GetLast(mailAdress);

            if (registration == null)
            {
                return(Ok(false));
            }

            var route = _routeRepository.GetBy(registration.RouteId);

            if (route == null)
            {
                return(NotFound("No route found"));
            }

            return(Ok(DateCheckHelper.CheckAfterOrEqualsToday(route.Date)));
        }
예제 #2
0
        public IActionResult Post(RouteRegistrationDTO registrationDTO)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Unauthorized());
            }

            var validation = _routeRegistrationDTOValidator.Validate(registrationDTO);

            if (!validation.IsValid)
            {
                return(BadRequest(validation));
            }

            string mailAdress = User.Identity.Name;

            if (mailAdress == null)
            {
                return(BadRequest());
            }

            var user = _userRepository.GetBy(mailAdress);

            if (user == null)
            {
                return(BadRequest());
            }

            var route = _routeRepository.GetBy(registrationDTO.RouteId);

            if (route == null)
            {
                return(NotFound("Chosen route could not be found."));
            }

            if (DateCheckHelper.CheckBeforeToday(route.Date))
            {
                return(BadRequest("You cannot register for a route in the past."));
            }

            var last = _registrationRepository.GetLast(mailAdress);

            if (last != null)
            {
                var lastRouteDate = _routeRepository.GetBy(last.RouteId).Date;
                if (DateCheckHelper.CheckAfterOrEqualsToday(lastRouteDate))
                {
                    return(BadRequest("You are already registered for a route this year."));
                }
            }

            var registration = registrationDTO.MapToRegistration(user, route);

            _registrationRepository.Add(registration, mailAdress);
            _userRepository.Update(user);

            var mailDTO = (user, route).MapToDTO();

            _mailService.SendRegistrationConfirmation(mailDTO);

            return(Ok(registration));
        }