Beispiel #1
0
        await _practiceTests.Find <PracticeTest>(practiceTest => practiceTest.ID == id).FirstOrDefaultAsync(); //? Same idea here as FindAsync vs Find & ToListAsync

        public async Task <PracticeTest> Create(PracticeTest practiceTest)                                     // Could also just forget the return making //@returns Task or void with a 1-liner body
        {
            //? The moment await is called, the func is paused. To avoid pausing, you could assign an async func to a task var instead,
            //? but when you need an async func to be successful before doing anything else. Await it!
            await _practiceTests.InsertOneAsync(practiceTest);

            return(practiceTest);
        }
Beispiel #2
0
        [HttpPut("{id:length(24)}")] // Use PATCH to do partial updates (PUT requires the entire object to edit)
        public async Task <IActionResult> PutPracticeTest(string id, PracticeTest practiceTest)
        {
            var testFromDB = await _practiceTestService.Get(id);

            if (testFromDB == null)
            {
                return(NotFound());              // Could also use a BadRequest!
            }
            await _practiceTestService.Update(id, practiceTest);

            return(NoContent());
        }
Beispiel #3
0
        // nameof = C# operator - takes a var, type, or member & gives a string representation (useful here to grab a func name ref)
        //@params 2. Specifies route to use (base it on ID) 3. Object to post
        //@returns 201 HTTP status (preferred for POST)
        public async Task <ActionResult <PracticeTest> > PostPracticeTest(PracticeTest practiceTest)
        {
            await _practiceTestService.Create(practiceTest);

            return(CreatedAtRoute("GetPracticeTest", new { id = practiceTest.ID }, practiceTest));
        }
Beispiel #4
0
 public async Task Remove(PracticeTest practiceTestIn) => // Delete based on obj
 await _practiceTests.DeleteOneAsync(practiceTest => practiceTest.ID == practiceTestIn.ID);
Beispiel #5
0
 //? Could return void BUT Task = better by enabling proper await. Void typically signifies a top level func or event handler
 public async Task Update(string id, PracticeTest practiceTestIn) => // Update based on ID and the obj itself. Obj required!
 await _practiceTests.ReplaceOneAsync(practiceTest => practiceTest.ID == id, practiceTestIn);