public IActionResult Post([FromBody] CarDTO dto)
        {
            Car car = new Car()
            {
                Name       = dto.Name,
                Path       = dto.Path,
                LogoImgUrl = dto.LogoImgUrl
            };

            _context.Cars.Add(car);
            _context.SaveChanges();
            return(Ok(car));
        }
Exemplo n.º 2
0
        public IActionResult ResetUser([FromRoute] string id)
        {
            var user = _context.Users
                       .Include(u => u.Invite)
                       .FirstOrDefault(u => u.Id == id);

            if (user == null)
            {
                return(NotFound("Unable to find user to reset invitation"));
            }

            _context.UserInvites.Remove(user.Invite);
            _context.SaveChanges();
            return(Ok());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> SendIracingVerification([FromBody] IracingIdWrapper wrapper)
        {
            if (String.IsNullOrEmpty(wrapper.IracingId))
            {
                return(BadRequest("Iracing Id must be populated"));
            }
            wrapper.IracingId = wrapper.IracingId.Trim();
            bool canConvert = int.TryParse(wrapper.IracingId, out _);

            if (!canConvert)
            {
                return(BadRequest("Iracing Id must be a number"));
            }

            var user = _context.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);

            if (user == null)
            {
                return(Unauthorized());
            }
            if (!String.IsNullOrEmpty(user.IracingId))
            {
                return(BadRequest("User has already been verified on iRacing"));
            }
            var existingUser = _context.Users.FirstOrDefault(u => u.IracingId == wrapper.IracingId);

            if (existingUser != null)
            {
                return(BadRequest($"User with iracing {wrapper.IracingId} has already been created"));
            }


            var invite = _context.UserInvites.FirstOrDefault(u => u.UserId == user.Id && u.Status == InviteStatus.SENT);

            if (invite == null)
            {
                var duplicateInvite =
                    _context.UserInvites.FirstOrDefault(u => u.IracingId == wrapper.IracingId && u.Status == InviteStatus.SENT);

                if (duplicateInvite != null)
                {
                    return(BadRequest("Someone else has claimed this iRacing account already. Please contact Support to resolve this"));
                }

                invite = new UserInvite
                {
                    UserId      = user.Id,
                    Status      = InviteStatus.SENT,
                    LastUpdated = DateTime.UtcNow,
                    IracingId   = wrapper.IracingId
                };
                _context.UserInvites.Add(invite);
            }
            else
            {
                DateTime now = DateTime.UtcNow;
                if (invite.LastUpdated > now.AddMinutes(-15) && invite.LastUpdated <= now)
                {
                    return(BadRequest("You must wait 15 minutes after a verification message to request an additional one"));
                }
                if (invite.IracingId != wrapper.IracingId)
                {
                    return(BadRequest("You have an existing invite with a different iRacing Id. Please contact Support to resolve this"));
                }
                invite.LastUpdated = DateTime.UtcNow;
            }

            try
            {
                var success = await _iracingService.SendPrivateMessage(wrapper.IracingId,
                                                                       $"Click on the link to validate your RaceSpot Liveries Account! {_baseUrl}/#key={invite.Id}");

                if (!success)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError,
                                      "An error occured sending a PM. Please contact Support for assistance."));
                }
                _context.SaveChanges();
                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  "There was an internal error that prevented a PM from being sent. Please contact Support for assistance."));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Post([FromRoute] Guid seriesId, [FromBody] LiveryDTO dto)
        {
            var user = await _context.Users.FirstOrDefaultAsync(u => u.UserName == User.Identity.Name);

            if (user == null)
            {
                return(Unauthorized());
            }
            else if (String.IsNullOrEmpty(user.IracingId))
            {
                return(Forbid("User has not verified iRacing account yet"));
            }
            if (!dto.CarId.HasValue && (dto.LiveryType == LiveryType.Car || dto.LiveryType == LiveryType.SpecMap))
            {
                return(BadRequest("Car livery must have carId in request"));
            }
            var series = await _context.Series.FirstOrDefaultAsync(s => s.Id == seriesId);

            if (series == null)
            {
                return(NotFound($"Could not find series with id {seriesId}"));
            }
            if (series.IsTeam && (String.IsNullOrEmpty(dto.ITeamId) && dto.LiveryType != LiveryType.Helmet))
            {
                return(BadRequest("Series is a team series but no Team ID was provided"));
            }
            if (!series.IsTeam && !String.IsNullOrEmpty(dto.ITeamId))
            {
                return(BadRequest("Series is not a team series but Team ID was provided"));
            }
            if (!series.IsLeague && dto.IsCustomNumber)
            {
                return(BadRequest("Custom Number paints are only permitted for league events"));
            }

            Livery livery;

            if (String.IsNullOrEmpty(dto.ITeamId))
            {
                livery = _context.Liveries.Include(l => l.Car)
                         .Include(l => l.Series)
                         .FirstOrDefault(l => l.SeriesId == seriesId &&
                                         l.UserId == user.Id && l.LiveryType == dto.LiveryType && String.IsNullOrEmpty(l.ITeamId));
            }
            else
            {
                livery = _context.Liveries.Include(l => l.Car)
                         .Include(l => l.Series)
                         .FirstOrDefault(l => l.SeriesId == seriesId &&
                                         l.ITeamId == dto.ITeamId && l.LiveryType == dto.LiveryType);
            }
            if (livery == null)
            {
                var teamName = "";
                if (!String.IsNullOrEmpty(dto.ITeamId))
                {
                    try
                    {
                        var iTeam = await _iracingService.LookupIracingTeamById(dto.ITeamId, false);

                        if (iTeam.TeamOwnerId != user.IracingId)
                        {
                            return(BadRequest("User is not owner of iRacing Team. Please ask Team Owner to upload livery"));
                        }
                        teamName = iTeam.TeamName;
                    }
                    catch (Exception ex)
                    {
                        return(StatusCode(StatusCodes.Status500InternalServerError,
                                          "Unable to verify user owns team. Please contact Support for assistance."));
                    }
                }
                livery = new Livery()
                {
                    SeriesId       = seriesId,
                    Series         = series,
                    ITeamId        = dto.ITeamId,
                    ITeamName      = teamName,
                    LiveryType     = dto.LiveryType,
                    User           = user,
                    UserId         = user.Id,
                    Status         = UploadStatus.WAITING,
                    IsCustomNumber = dto.IsCustomNumber,
                    IsRejected     = false,
                    LastUpdated    = DateTime.UtcNow,
                };
                if (dto.CarId.HasValue)
                {
                    var car = await _context.Cars.Include(c => c.SeriesCars).FirstOrDefaultAsync(c => c.Id == dto.CarId.Value);

                    if (car == null)
                    {
                        return(NotFound($"Could not find car with id {dto.CarId}"));
                    }
                    if (car.SeriesCars.Count(s => s.SeriesId == seriesId) == 0)
                    {
                        return(BadRequest("Car is not in series"));
                    }
                    livery.CarId = dto.CarId.Value;
                    livery.Car   = car;
                }
                _context.Liveries.Add(livery);
                _context.SaveChanges();
            }
            else
            {
                if (!String.IsNullOrEmpty(dto.ITeamId) && livery.UserId != user.Id)
                {
                    return(BadRequest("You are not the original uploader of the livery"));
                }
                if (dto.CarId.HasValue && dto.CarId.Value != livery.CarId)
                {
                    var car = await _context.Cars.Include(c => c.SeriesCars).FirstOrDefaultAsync(c => c.Id == dto.CarId.Value);

                    if (car == null)
                    {
                        return(NotFound($"Could not find car with id {dto.CarId}"));
                    }
                    if (car.SeriesCars.Count(s => s.SeriesId == seriesId) == 0)
                    {
                        return(BadRequest("Car is not in series"));
                    }
                    var specMap = _context.Liveries.FirstOrDefault(l => l.LiveryType == LiveryType.SpecMap && l.SeriesId == seriesId && l.ITeamId == livery.ITeamId);
                    if (specMap != null)
                    {
                        _context.Liveries.Remove(specMap);
                    }
                    livery.CarId = dto.CarId.Value;
                    livery.Car   = car;
                }
                if (dto.LiveryType == LiveryType.Car)
                {
                    livery.IsCustomNumber = dto.IsCustomNumber;
                }

                if (livery.IsRejected)
                {
                    var rejection = _context.Rejections
                                    .FirstOrDefault(r => r.LiveryId == livery.Id && r.Status != RejectionStatus.Resolved);
                    if (rejection == null)
                    {
                        return(StatusCode(StatusCodes.Status500InternalServerError,
                                          "Error while resetting rejected status on livery - please contact Support!"));
                    }
                    rejection.Status = RejectionStatus.Updated;
                }
                livery.Status = UploadStatus.WAITING;
                _context.SaveChanges();
            }

            var returnDto = new LiveryDTO(livery);

            returnDto.UploadUrl = _s3Service.GetPresignedPutUrlForLivery(livery);
            return(Ok(returnDto));
        }