public IActionResult Delete(int id)
        {
            GolfMatch golfMatch = _db.GolfMatches.Where(i => i.GolfMatchId == id).First();

            if (golfMatch == null)
            {
                return(HttpNotFound());
            }

            _db.GolfMatches.Remove(golfMatch);
            _db.SaveChanges();

            return(Ok(golfMatch));
        }
Exemple #2
0
        public GolfMatchDto(GolfMatch golfMatch)
        {
            GolfMatchId      = golfMatch.GolfMatchId;
            StartDate        = golfMatch.StartDate;
            Comments         = golfMatch.Comments;
            NumberOfHoles    = golfMatch.NumberOfHoles;
            NumberOfPlayers  = golfMatch.NumberOfPlayers;
            Time             = golfMatch.Time;
            GolfMatchDateIso = golfMatch.GolfMatchDateIso;

            golfMatch.Players.ToList().ForEach(p => Players.Add(new GolfMatchPlayerDto()
            {
                DisplayName    = p.User.FirstName + " " + p.User.LastName,
                Email          = p.User.Email,
                Handicap       = p.User.Handicap,
                IsMatchCreator = p.IsMatchCreator,
            }));
        }
        public IActionResult PostGolfMatch([FromBody] GolfMatchDto match)
        {
            if (!ModelState.IsValid)
            {
                return(HttpBadRequest(ModelState));
            }

            try
            {
                var createdByUser = userManager.FindByNameAsync(match.UserName).Result;

                // get club id of selected club
                var club = _db.Clubs.Where(c => c.Name == "BakerHill").Single();

                var newGolfMatch = new GolfMatch()
                {
                    GolfMatchDateIso = DateTime.Parse(match.StartDate + " " + match.Time).ToUniversalTime().ToString("o"),
                    StartDate        = match.StartDate,
                    Time             = DateTime.Parse(match.Time).ToString("HH:mm:ss"),
                    NumberOfPlayers  = 4,
                    NumberOfHoles    = match.NumberOfHoles,
                    Comments         = match.Comments,
                    ClubId           = club.ClubId
                };

                _db.GolfMatches.Add(newGolfMatch);
                _db.SaveChanges();

                foreach (var player in match.Players)
                {
                    var appPlayer = GetPlayer(player);

                    if (appPlayer != null)
                    {
                        bool isMatchCreator = false;
                        if (appPlayer == createdByUser)
                        {
                            isMatchCreator = true;
                        }

                        _db.GolfMatchPlayers.Add(new GolfMatchPlayer()
                        {
                            GolfMatchId    = newGolfMatch.GolfMatchId,
                            User           = appPlayer,
                            IsMatchCreator = isMatchCreator,
                            InviteStatus   = new InviteStatus()
                            {
                                Name = isMatchCreator ? InviteStatusName.Accepted.ToString() : InviteStatusName.Pending.ToString()
                            },
                        });
                    }
                }

                _db.SaveChanges();

                return(new ObjectResult(newGolfMatch));
            }
            catch (Exception ex)
            {
                // Log.Error(e.Message);
                return(new ObjectResult(ex.Message));
            }
        }