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")); } }
public async Task <ActionResult <ProgrammeDto> > PostProgrammeAsync([FromBody] ProgrammeDto programme) { if (programme.Description == programme.Name) { // Business rule - Name and Description must be different ModelState.AddModelError(programme.Description, $"Name: {programme.Name} and description: {programme.Description} should be different."); } if (!ModelState.IsValid) { _logger.LogError("Partial update failed."); return(BadRequest(ModelState)); } var programmeToDb = _mapper.Map <Programme>(programme); try { var addedProgramme = await _programmeRepository.AddAsync(programmeToDb); await _programmeRepository.SaveChangesAsync(); var programmeToReturn = _mapper.Map <ProgrammeDto>(addedProgramme); return(CreatedAtAction("GetProgramme", new { id = addedProgramme.ProgrammeId }, programmeToReturn)); } catch (DataAccessException e) { _logger.LogError(e, "Post programme failed."); return(BadRequest("Post programme failed.")); } catch (Exception e) { _logger.LogError(e, "Post programme failed."); return(BadRequest("Post programme failed.")); } }