コード例 #1
0
        public async Task <IActionResult> Create([Bind("ProgrammeID,ProgrammeName,ProgrammeDescription,ProgrammeQQILevel,ProgrammeCredits,ProgrammeCost")] Programme programme)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    await programmeRepository.AddAsync(programme);

                    await programmeRepository.SaveChangesAsync();

                    ViewBag.SuccessMsg = "Programme Created Successfully";

                    var programmeDetails = await programmeRepository.FindByIdAsync(programme.ProgrammeID);

                    ModelState.Clear();

                    return(RedirectToAction("Details", new { id = programme.ProgrammeID }));
                }
                return(View(programme));
            }
            catch (DataAccessException e)
            {
                ViewBag.ErrorMsg = ErrorProcessing.ProcessException("Data Access exception. ", e);
                return(RedirectToAction("Unknown", "Error"));
            }
            catch (Exception e)
            {
                ViewBag.ErrorMsg = ErrorProcessing.ProcessException("General exception. ", e);
                return(RedirectToAction("Unknown", "Error"));
            }
        }
コード例 #2
0
        public async Task <IActionResult> PutProgrammeAsync(string id,
                                                            [FromBody] ProgrammeDto programme)
        {
            if (id != programme.ProgrammeId)
            {
                _logger.LogWarning($"Put programme. Id {id} does not match {programme.ProgrammeId}.");
                return(BadRequest());
            }

            try
            {
                var programmeFromDb = await _programmeRepository.FindByIdAsync(id);

                if (programmeFromDb == null)
                {
                    _logger.LogWarning($"Get programme. Id {id} not found.");
                    return(NotFound($"Programme {id} not found"));
                }

                // Map all properties except Id
                programmeFromDb.Name        = programme.Name;
                programmeFromDb.Description = programme.Description;

                var result = _programmeRepository.Update(programmeFromDb);
                if (result == null)
                {
                    _logger.LogWarning($"Put programme. Id {id} not found.");
                    return(NotFound());
                }
                await _programmeRepository.SaveChangesAsync();
            }
            catch (DataAccessException e)
            {
                _logger.LogCritical(e, "Update failed.");
                return(BadRequest($"Update of programme {id} failed."));
            }
            return(NoContent());
        }