public IHttpActionResult Post([FromBody] Tournament tournament)
        {
            var result = new Tournament();

            if (ModelState.IsValid)
            {
                if (tournament.Organizer == null)
                {
                    return(Content(HttpStatusCode.BadRequest, "Organizer is empty"));
                }

                if (tournament.Organizer.Id == 0 || organizerRepository.Get(tournament.Organizer.Id) == null)
                {
                    return(Content(HttpStatusCode.NotFound, "Organizer not found"));
                }

                if (tournament.Venue == null)
                {
                    return(Content(HttpStatusCode.BadRequest, "Venue is empty"));
                }

                if (tournament.Venue.Id == 0 || venueRepository.Get(tournament.Venue.Id) == null)
                {
                    return(Content(HttpStatusCode.NotFound, "Venue not found"));
                }

                result = tournamentRepository.Add(tournament);
            }

            return(Json(result));
        }
        public Tournament Add(Tournament tournament)
        {
            Organizer organizer;

            if (tournament.Organizer == null)
            {
                organizer = organizerRepository.Add(tournament.Organizer);
            }
            else
            {
                organizer = organizerRepository.Get(tournament.Organizer.Id);
            }

            Venue venue;

            if (tournament.Venue == null)
            {
                venue = venueRepository.Add(tournament.Venue);
            }
            else
            {
                venue = venueRepository.Get(tournament.Venue.Id);
            }

            ICollection <Category> categories = new List <Category>();

            if (tournament.Categories != null && tournament.Categories.Count > 0)
            {
                foreach (Category category in tournament.Categories)
                {
                    var categoryFromDb = categoryRepository.Get(category.Id);

                    if (categoryFromDb == null)
                    {
                        categories.Add(categoryRepository.Add(category));
                    }
                    else
                    {
                        categories.Add(categoryFromDb);
                    }
                }
            }

            tournament.Organizer  = organizer;
            tournament.Venue      = venue;
            tournament.Categories = categories;

            var result = context.Tournaments.Add(tournament);

            Save();

            return(result);
        }
 public IHttpActionResult Get(int id)
 {
     return(Json(organizerRepository.Get(id)));
 }