public IHttpActionResult PutComp_Result(int id, Comp_Result comp_Result)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != comp_Result.CompResultID)
            {
                return(BadRequest("Comp Result with that ID doesn't Exist"));
            }

            // commented line for unit tests and added the mark as modified, reverse before deploying
            //db.MarkAsModified(comp_Result);
            db.Entry(comp_Result).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!Comp_ResultExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult GetCompResultID(int id)
        {
            Comp_Result comp_Result = db.Comp_Results.Find(id);

            if (comp_Result == null)
            {
                return(NotFound());
            }

            return(Ok(comp_Result));
        }
Exemple #3
0
        public ActionResult Create([Bind(Include = "CompResultID,CompetitionID,StartDate,EndDate,GolferID,Position,Season,GolferScore")] Comp_Result comp_Result) // Bind makes sure only the variables mentioned are entered
        {
            if (ModelState.IsValid)
            {
                db.Comp_Results.Add(comp_Result);
                db.SaveChanges();
                return(RedirectToAction("ResultUploaded"));
                //return RedirectToAction("Index");
            }

            ViewBag.CompetitionID = new SelectList(db.Competitions, "ID", "Name", comp_Result.CompetitionID);
            ViewBag.GolferID      = new SelectList(db.Golfers, "ID", "Firstname", comp_Result.GolferID);
            return(View(comp_Result));
        }
        public IHttpActionResult DeleteComp_Result(int id)
        {
            Comp_Result comp_Result = db.Comp_Results.Find(id);

            if (comp_Result == null)
            {
                return(NotFound());
            }

            db.Comp_Results.Remove(comp_Result);
            db.SaveChanges();

            return(Ok(comp_Result));
        }
        public IHttpActionResult PostComp_Result(Comp_Result comp_Result)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // Checks for Comp ID match, if matches checks for season matches, if matches checks for golfer ID. All three connditions need to be met
            var foundCompRes = db.Comp_Results.Where(x => x.CompetitionID == comp_Result.CompetitionID && x.Season == comp_Result.Season && x.GolferID == comp_Result.GolferID).FirstOrDefault();

            if (foundCompRes != null)
            {
                return(BadRequest("Competition Results for that Golfer, Comp ID and Year already exists"));
            }

            db.Comp_Results.Add(comp_Result);
            db.SaveChanges();

            return(CreatedAtRoute("AddCompResult", new { id = comp_Result.CompResultID }, comp_Result));
        }
        static async Task GetsCompResultsAsync()
        {
            //GET api/CompResult/all
            HttpResponseMessage response = await golfResultsAPI.GetAsync("api/CompResult/all");

            if (response.IsSuccessStatusCode)
            {
                var returnedCompResults = await response.Content.ReadAsAsync <IEnumerable <Comp_Result> >();

                Console.WriteLine("\n*** Complete Comp List ****");
                int i = 0;
                foreach (Comp_Result g in returnedCompResults)
                {
                    i += 1;
                    Console.WriteLine(i + ". " + g);
                }
            }

            //GET api/CompResult/GetCompResultID/1
            response = await golfResultsAPI.GetAsync("api/CompResult/GetCompResultID/1");

            if (response.IsSuccessStatusCode)
            {
                var returnedCompResult = await response.Content.ReadAsAsync <Comp_Result>(); // add from Console line

                Console.WriteLine("\nComp Result with ID 1: " + returnedCompResult);
            }

            //GET api/CompResult/GetCompID/1
            response = await golfResultsAPI.GetAsync("api/CompResult/GetCompID/1");

            if (response.IsSuccessStatusCode)
            {
                var returnedCompResult = await response.Content.ReadAsAsync <IEnumerable <Comp_Result> >(); // add from Console line

                Console.WriteLine("\nGolf Results with Comp ID 1\n");
                foreach (Comp_Result g in returnedCompResult)
                {
                    Console.WriteLine(g);
                }
            }

            //GET api/CompResult/GetCompResultPerSeason/1/2019
            response = await golfResultsAPI.GetAsync("api/CompResult/GetCompResultPerSeason/1/2019");

            if (response.IsSuccessStatusCode)
            {
                var returnedCompResult = await response.Content.ReadAsAsync <IEnumerable <Comp_Result> >(); // add from Console line

                Console.WriteLine("\nGolf Results for Comp ID 1 for 2019 Season");
                foreach (Comp_Result g in returnedCompResult)
                {
                    Console.WriteLine(g);
                }
            }

            //POST api/CompResult/PostComp
            Comp_Result c1 = new Comp_Result()
            {
                CompetitionID = 1, Season = 2020, StartDate = new DateTime(2020, 01, 01), EndDate = new DateTime(2020, 01, 05), GolferID = 12, GolferScore = "+5", Position = "6"
            };

            response = golfResultsAPI.PostAsJsonAsync("api/CompResult/PostComp_Result", c1).Result;
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("\nAdded Golf Comp Result: " + c1);
            }

            //PUT api/CompResult/PutComp_Result/1
            Comp_Result c2 = new Comp_Result()
            {
                CompResultID = 23, CompetitionID = 1, Season = 2020, StartDate = new DateTime(2020, 01, 01), EndDate = new DateTime(2020, 01, 05), GolferID = 11, GolferScore = "DNF", Position = "MC"
            };

            response = golfResultsAPI.PutAsJsonAsync("api/CompResult/PutComp_Result/24", c2).Result;
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("\nUpdated Golf Comp Result: " + c2);
            }

            //DELETE api/CompResult/DeleteComp_Result/1
            response = golfResultsAPI.DeleteAsync("api/CompResult/DeleteComp_Result/19").Result;
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("\nDeleted Golf Comp Result: " + response.IsSuccessStatusCode);
            }
        }