public int SaveTeam(TeamDto team) { Team teamEntity = null; if (team.Id == 0) { teamEntity = new Team {CreatedDate = DateTime.UtcNow, Name = team.Name, CreatedByID = team.CreatedById}; db.Teams.Add(teamEntity); } else { teamEntity = db.Teams.FirstOrDefault(s => s.ID == team.Id); if (teamEntity != null) { teamEntity.Name = team.Name; db.Entry(team).State = EntityState.Modified; } } db.SaveChanges(); return teamEntity.ID; }
public ActionResult Create(TeamVM model) { try { if (ModelState.IsValid) { Team team = new Team { Name = model.Name, ID = model.Id }; bool isNew = (model.Id == 0); if (!isNew) { team = repo.GetTeam(model.Id); team.Name = model.Name; } else { team.CreatedByID = UserID; } var result = repo.SaveTeam(team); if (result != null) { if (isNew) { var teamMember = new TeamMember { MemberID = UserID, TeamID = team.ID, CreatedByID = UserID }; repo.SaveTeamMember(teamMember); } return Json(new { Status = "Success" }); } } return View(model); } catch(Exception ex) { log.Error("Error updating team "+model.Id, ex); } return Json(new { Status = "Error" }); }
public Team SaveTeam(Team team) { if (team.ID == 0) { team.CreatedDate = DateTime.UtcNow; db.Teams.Add(team); } else { db.Entry(team).State = EntityState.Modified; } db.SaveChanges(); return team; }