public async Task <ActionResult> InsertTeam(CRUDModel <TeamViewModel> team) { try { if (!ModelState.IsValid) { var message = string.Join(" | ", ModelState.Values .SelectMany(v => v.Errors) .Select(e => e.ErrorMessage)); return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, message)); } var usersRolesLanguages = await _applicationUsersService.GetUsersAndRolesAndLanguages(); var newTeam = new Team { Name = team.Value.TeamName }; List <string> fullnames = new List <string>(); if (team.Value.Fullname.Count != 0) { foreach (var user in team.Value.Fullname[0].Split(',')) { if (string.IsNullOrWhiteSpace(user)) { continue; } var userFound = usersRolesLanguages.Users.First(u => u.UserId == Convert.ToInt32(user)); newTeam.Users.Add(userFound); fullnames.Add(userFound.FullName); } } IEnumerable <Team> addTeam = new[] { newTeam }; await _applicationUsersService.SaveTeams(addTeam); var teams = await GetData(); var createdTeam = teams.TeamViewModel.First(u => u.TeamName == newTeam.Name); team.Value.TeamId = createdTeam.TeamId; team.Value.Fullname = fullnames; return(Json(team.Value, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { var message = string.Join(" | ", ModelState.Values .SelectMany(v => v.Errors) .Select(e => e.ErrorMessage)); if (ex.Message == "Common_CannotUseSameName") { message = LocalizedStrings.GetString("Common_CannotUseSameName"); } return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, message)); } }
public async Task <IHttpActionResult> SaveTeams([DynamicBody] dynamic param) { try { IEnumerable <Ksmed.Models.Team> teams = param.teams; return(Ok(await _applicationUsersService.SaveTeams(teams))); } catch (Exception ex) { _traceManager.TraceError(ex, ex.Message); return(InternalServerError(ex)); } }