public GolfClubInformation SaveGolfClub([FromBody] GolfClubInformation golfClubToSave)
        {
            const string message = "Could not save golf club.";

            try
            {
                using (var context = new GolfTrackerContext())
                {
                    if (golfClubToSave.Id == Guid.Empty)
                    {
                        golfClubToSave.Id = Guid.NewGuid();
                    }

                    var golfClub = context.GolfClub
                                   .Include("GolfCourses.Tees")
                                   .FirstOrDefault(c => c.Id == golfClubToSave.Id);

                    if (golfClub == null)
                    {
                        golfClub = new GolfClub {
                            Id = golfClubToSave.Id
                        };
                        context.GolfClub.Add(golfClub);
                    }

                    mapInformationToEntity(golfClubToSave, golfClub, context);

                    context.SaveChanges();

                    return(mapEntityToInformation(golfClub));
                }
            }
            catch (Exception ex)
            {
                var exMessage = ExceptionHelper.ExceptionToString(ex, message);
                _logger.LogCritical(exMessage);
                return(null);
            }
        }
Esempio n. 2
0
        public GolferInformation SaveGolfer([FromBody] GolferInformation golferToSave)
        {
            const string message = "Could not save golfer.";

            try
            {
                using (var context = new GolfTrackerContext())
                {
                    if (golferToSave.Id == Guid.Empty)
                    {
                        golferToSave.Id = Guid.NewGuid();
                    }

                    var golfer = context.Golfer
                                 .Include("Rounds.GolfCoursePlayed.TeesPlayed")
                                 .FirstOrDefault(g => g.Id == golferToSave.Id);

                    if (golfer == null)
                    {
                        golfer = new Golfer {
                            Id = golferToSave.Id
                        };
                        context.Golfer.Add(golfer);
                    }

                    mapInformationToEntity(golferToSave, golfer, context);

                    context.SaveChanges();

                    return(mapEntityToInformation(golfer));
                }
            }
            catch (Exception ex)
            {
                var exMessage = ExceptionHelper.ExceptionToString(ex, message);
                _logger.LogCritical(exMessage);
                return(null);
            }
        }
        public void DeleteGolfClub(Guid id)
        {
            const string message = "Could not delete golf club.";

            try
            {
                using (var context = new GolfTrackerContext())
                {
                    var golfClub = context.GolfClub
                                   .FirstOrDefault(c => c.Id == id);

                    if (golfClub != null)
                    {
                        context.GolfClub.Remove(golfClub);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                var exMessage = ExceptionHelper.ExceptionToString(ex, message);
                _logger.LogCritical(exMessage);
            }
        }