public static async Task<UserAccounts> RegisterUser(ToracGolfContext dbContext, SignUpEnteredDataBase signUpModel) { //go either get the ref season record or add it var refSeasonRecord = await SeasonDataProvider.RefSeasonAddOrGet(dbContext, signUpModel.CurrentSeason).ConfigureAwait(false); //user to add var userToAdd = new UserAccounts { StateId = Convert.ToInt32(signUpModel.HomeState), CreatedDate = DateTime.Now, CurrentSeasonId = refSeasonRecord.SeasonId, EmailAddress = signUpModel.Email, FirstName = signUpModel.FirstName, LastName = signUpModel.LastName, Password = signUpModel.Password }; //go try to add the user dbContext.Users.Add(userToAdd); //go save the changes await dbContext.SaveChangesAsync().ConfigureAwait(false); //add the user season now await SeasonDataProvider.UserSeasonAdd(dbContext, userToAdd.UserId, refSeasonRecord).ConfigureAwait(false); //return the user now return await dbContext.Users.AsNoTracking().FirstAsync(x => x.UserId == userToAdd.UserId).ConfigureAwait(false); }
public static async Task ChangePassword(ToracGolfContext dbContext, int userId, string password) { var user = await dbContext.Users.FirstAsync(x => x.UserId == userId).ConfigureAwait(false); user.Password = password; await dbContext.SaveChangesAsync().ConfigureAwait(false); }
public static async Task<int> CourseAdd(ToracGolfContext dbContext, int userId, CourseAddEnteredData CourseData, string courseSavePath) { //course record to add var courseToAdd = new Course { Name = CourseData.CourseName, Description = CourseData.Description, IsActive = true, Pending = true, City = CourseData.City, StateId = Convert.ToInt32(CourseData.StateListing), UserIdThatCreatedCourse = userId, OnlyAllow18Holes = CourseData.OnlyAllow18Holes, CreatedDate = DateTime.Now }; courseToAdd.CourseTeeLocations = CourseData.TeeLocations.Select((x, i) => new CourseTeeLocations { CourseId = courseToAdd.CourseId, Description = x.Description, TeeLocationSortOrderId = i, Front9Par = x.Front9Par.Value, Back9Par = x.Back9Par.Value, Rating = x.Rating.Value, Slope = x.Slope.Value, Yardage = x.Yardage.Value, FairwaysOnCourse = 18 - x.NumberOfPar3s.Value }).ToArray(); //add the course to the context dbContext.Course.Add(courseToAdd); //go save it await dbContext.SaveChangesAsync().ConfigureAwait(false); //do we have a course image? if (CourseData.CourseImage != null) { //grab the byte array for the file var fileToSave = ToracLibrary.Core.Graphics.GraphicsUtilities.ImageFromJsonBase64String(CourseData.CourseImage); //grab where we have the actual .jpg vs png var indexOfSlash = fileToSave.MimeType.IndexOf(@"/"); //grab the file name to save var fileNameToSave = System.IO.Path.Combine(courseSavePath, string.Format("{0}.{1}", courseToAdd.CourseId, fileToSave.MimeType.Substring(indexOfSlash + 1, fileToSave.MimeType.Length - indexOfSlash - 1))); //go save the file System.IO.File.WriteAllBytes(fileNameToSave, fileToSave.FileBytes); } //return the course id return courseToAdd.CourseId; }
public static async Task<UserSeason> UserSeasonAdd(ToracGolfContext dbContext, int userId, Ref_Season refSeasonRecord) { //record to add var recordToAdd = new UserSeason { UserId = userId, SeasonId = refSeasonRecord.SeasonId, CreatedDate = DateTime.Now }; //add the record dbContext.UserSeason.Add(recordToAdd); //go save the changes await dbContext.SaveChangesAsync().ConfigureAwait(false); //return the record now return recordToAdd; }
public static async Task<Ref_Season> RefSeasonAddOrGet(ToracGolfContext dbContext, string seasonText) { //let's first try to find the season with the same name var seasonToAdd = await dbContext.Ref_Season.AsNoTracking().FirstOrDefaultAsync(x => x.SeasonText == seasonText).ConfigureAwait(false); //didn't find a season with this name if (seasonToAdd == null) { //create the new season seasonToAdd = new Ref_Season { SeasonText = seasonText, CreatedDate = DateTime.Now }; //add the record dbContext.Ref_Season.Add(seasonToAdd); //we need to save it for foreign key await dbContext.SaveChangesAsync().ConfigureAwait(false); } //return the record return seasonToAdd; }
public static async Task<bool> DeleteARound(ToracGolfContext dbContext, int roundIdToDelete) { //grab the round var roundToDelete = await dbContext.Rounds.FirstOrDefaultAsync(x => x.RoundId == roundIdToDelete); //remove the round dbContext.Rounds.Remove(roundToDelete); //save the changes await dbContext.SaveChangesAsync().ConfigureAwait(false); //return a true result return true; }
public static async Task<bool> RefreshHandicapRecords(ToracGolfContext dbContext, int userId) { //let's go delete all the handicap records var roundsToDelete = await (from myData in dbContext.Rounds join myHandicaps in dbContext.Handicap on myData.RoundId equals myHandicaps.RoundId where myData.UserId == userId select myHandicaps).ToArrayAsync(); //delete all handicap records dbContext.Handicap.RemoveRange(roundsToDelete); //save the deletions await dbContext.SaveChangesAsync(); //5. let's go rebuild all the handicap records var roundsForUser = await dbContext.Rounds.Include(x => x.CourseTeeLocation).Where(x => x.UserId == userId) .OrderBy(x => x.RoundDate).ThenBy(x => x.RoundId) .Select(x => x).ToArrayAsync(); //func to go from rounds to last 20 rounds Func<Round, Last20Rounds> roundToLast20 = x => new Last20Rounds { RoundId = x.RoundId, RoundScore = x.Score, Rating = x.CourseTeeLocation.Rating, Slope = x.CourseTeeLocation.Slope }; //lets loop through those rounds for the user foreach (var roundToCalculate in roundsForUser) { //grab the 20 rounds before this round var roundsToUse = roundsForUser .Where(x => x.RoundDate < roundToCalculate.RoundDate) .OrderByDescending(x => x.RoundDate) .ThenByDescending(x => x.RoundId) .Take(20) .Select(roundToLast20).ToArray(); //current round we are up too var currentRoundInArrayFormat = new Last20Rounds[] { roundToLast20(roundToCalculate) }; //rounds for the "After round handicap" ICollection<Last20Rounds> roundsToCalculateAfterRoundHandicap; //if we have 0 rounds, then use the round we are looking through if (!roundsToUse.Any()) { //set the current rounds to just this one guy roundsToUse = currentRoundInArrayFormat; //set it to just this round roundsToCalculateAfterRoundHandicap = roundsToUse; } else { roundsToCalculateAfterRoundHandicap = roundsToUse.Concat(currentRoundInArrayFormat).ToArray(); } //add the record to the context dbContext.Handicap.Add(new Handicap { RoundId = roundToCalculate.RoundId, HandicapBeforeRound = Handicapper.CalculateHandicap(roundsToUse).Value, HandicapAfterRound = Handicapper.CalculateHandicap(roundsToCalculateAfterRoundHandicap).Value }); } //go save the changes for the course handicap await dbContext.SaveChangesAsync(); //return a postive result return true; }
public static async Task<bool> DeleteACourse(ToracGolfContext dbContext, int courseId) { //grab the course var courseToDelete = await dbContext.Course.FirstAsync(x => x.CourseId == courseId).ConfigureAwait(false); //now flip the flag on the course courseToDelete.IsActive = false; //save the changes await dbContext.SaveChangesAsync().ConfigureAwait(false); //return a positive result return true; }
public static async Task<bool> MakeSeasonAsCurrent(ToracGolfContext dbContext, int userId, int currentSeasonId) { //grab the user record var user = await dbContext.Users.FirstAsync(x => x.UserId == userId).ConfigureAwait(false); //set the current season now user.CurrentSeasonId = currentSeasonId; //save the changes await dbContext.SaveChangesAsync().ConfigureAwait(false); //return a postive result return true; }
public static async Task<bool> DeleteSeason(ToracGolfContext dbContext, int userId, int seasonIdToDelete) { //go grab the season to delete var seasonToDelete = await dbContext.UserSeason.FirstAsync(x => x.UserId == userId && x.SeasonId == seasonIdToDelete).ConfigureAwait(false); //remove it from the context dbContext.UserSeason.Remove(seasonToDelete); //save it now await dbContext.SaveChangesAsync().ConfigureAwait(false); //return a postive result return true; }