public async Task <IActionResult> Edit(int id, [Bind("Id,UserId,DisciplineId")] UserDiscipline userDiscipline) { if (id != userDiscipline.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(userDiscipline); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserDisciplineExists(userDiscipline.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(userDiscipline)); }
public async Task <IActionResult> Create([Bind("Id,UserId,DisciplineId")] UserDiscipline userDiscipline) { if (ModelState.IsValid) { _context.Add(userDiscipline); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(userDiscipline)); }
public async Task <UserDiscipline> CreateDiscipline(string username, UserDiscipline ud) { var sql = @" declare @num int; set @num = (select count(distinct DisciplineId) from UserWorksDiscipline where UserId = (select Id from Users where Username = @Username)); if @num < 5 insert into UserWorksDiscipline (UserId, DisciplineId, Year) values ((select Id from Users where Username = @Username), (select Id from Disciplines where Name = @Discipline), RTRIM(LTRIM(@Year))); ELSE THROW 51000, 'Cannot Add More Than 5 Disciplines.', 1; ;"; using var connection = new SqlConnection(connectionString); connection.Open(); await connection.ExecuteAsync(sql, new { Username = username, Discipline = ud.Discipline, Year = ud.YOE }); return(ud); }
public async Task <ActionResult <UserDiscipline> > CreateDiscipline([FromRoute] string username, [FromBody] UserDiscipline ud) { var response = await udRepository.CreateDiscipline(username, ud); var viewModel = mapper.Map <UserDiscipline>(response); return(Created("GetDiscipline", viewModel)); }