public async Task PostCreate_ForDublicatedPositions_ReturnsError() { var mockService = new Mock <IBetService>(); var controller = new BetsGroupController(mockService.Object, null); var model = new BetGroupBindingModel() { Teams = new List <BetTeamBindingModel>() { new BetTeamBindingModel() { Position = 1 }, new BetTeamBindingModel() { Position = 1 } } }; var result = await controller.Create(model, 1); var viewResult = result as ViewResult; Assert.IsTrue(Validator.ModelStateHasError(viewResult, "No dublicate positions are allowed.")); }
public async Task <IActionResult> EditPosition(BetGroupBindingModel model, int id, string returnUrl = "/") { if (!ModelState.IsValid) { this.ModelState.AddModelError(string.Empty, "Validation error."); return(View(moderationService.PrepareGroupScore(id))); } var distTeams = model.Teams.Select(x => x.Position).Distinct().ToArray(); if (distTeams.Length != model.Teams.Count) { this.ModelState.AddModelError(string.Empty, "No dublicate positions are allowed."); return(View(moderationService.PrepareGroupScore(id))); } try { var result = await moderationService.EditGroupScoreAsync(id, model); if (result != null) { this.ModelState.AddModelError(string.Empty, result); return(View(new BetGroupBindingModel())); } return(Redirect(returnUrl)); } catch { return(Redirect("/")); } }
public async Task <string> EditGroupScoreAsync(int groupId, BetGroupBindingModel model) { foreach (var team in model.Teams) { var dbTeam = await this.DbContext.Teams.FindAsync(team.TeamId); if (dbTeam == null) { return("Team not foud."); } dbTeam.GroupPosition = team.Position; } await this.DbContext.SaveChangesAsync(); return(null); }
public async Task <string> AddBetGroupAsync(string userId, BetGroupBindingModel model) { foreach (var team in model.Teams) { var bet = new BetPosition() { Position = team.Position, UserId = userId, TeamId = team.TeamId }; await this.DbContext.BetsForPosition.AddAsync(bet); } await this.DbContext.SaveChangesAsync(); return(null); }
public BetGroupBindingModel GetBetGroup(int groupId, string userId) { var teams = this.DbContext.BetsForPosition .Where(x => x.Team.GroupId == groupId && x.UserId == userId) .Select(x => new BetTeamBindingModel() { Position = x.Position, Name = x.Team.Name, TeamId = x.TeamId }) .ToArray(); var group = new BetGroupBindingModel() { Teams = teams }; return(group); }
public async Task <string> EditBetGroupAsync(string userId, BetGroupBindingModel model) { foreach (var team in model.Teams) { var bet = this.DbContext.BetsForPosition .Where(x => x.TeamId == team.TeamId && x.UserId == userId) .SingleOrDefault(); if (bet == null) { return("Bet not found."); } bet.Position = team.Position; } await this.DbContext.SaveChangesAsync(); return(null); }
public async Task <IActionResult> Create(BetGroupBindingModel model, int id, string returnUrl = "/") { if (!ModelState.IsValid) { this.ModelState.AddModelError(string.Empty, "Validation error."); return(View(betService.PrepareBetGroup(id))); } if (await betService.IsBeggined(true, id)) { this.ModelState.AddModelError(string.Empty, "Time is out for prognosis."); return(View(betService.PrepareBetGroup(id))); } var distTeams = model.Teams.Select(x => x.Position).Distinct().ToArray(); if (distTeams.Length != model.Teams.Count) { this.ModelState.AddModelError(string.Empty, "No dublicate positions are allowed."); return(View(betService.PrepareBetGroup(id))); } try { var currentUserId = userManager.GetUserId(this.User); var result = await betService.AddBetGroupAsync(currentUserId, model); if (result != null) { this.ModelState.AddModelError(string.Empty, result); return(View(betService.PrepareBetGroup(id))); } return(Redirect(returnUrl)); } catch { this.ModelState.AddModelError(string.Empty, "Error creating prognosis."); return(View(betService.PrepareBetGroup(id))); } }
public async Task AddBetGroup_WithFewTeams_AddsCorrectNumber() { var model = new BetGroupBindingModel() { Teams = new List <BetTeamBindingModel>() { new BetTeamBindingModel() { Position = 1 } } }; var service = new BetService(this.dbContext, null); var result = await service.AddBetGroupAsync(null, model); var betsCount = this.dbContext.BetsForPosition.Count(); Assert.AreEqual(model.Teams.Count, betsCount); }