/// <summary> /// Cancel a user fb /// </summary> /// <param name="order">The info to cancel the user fb</param> /// See <see cref="Areas.Bet.Models.CancelUserFootballBet"/> to know the param structure /// <returns>The IActionResult of the cancel a user fb action</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public IActionResult cancelUserFootballBet([FromBody] CancelUserFootballBet order) { User caller = TokenUserManager.getUserFromToken(HttpContext, _context); if (!caller.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(caller, _context)) { return(BadRequest("notAllowed")); } UserGroup ugCaller = new UserGroup(); Group group = new Group(); FootballBet footballBet = new FootballBet(); UserFootballBet userFootballBet = new UserFootballBet(); if (!UserInFootballBet.check(caller, ref group, order.groupName, ref ugCaller, ref footballBet, order.footballBet, _context, false)) { return(BadRequest()); } if (footballBet.cancelled) { return(BadRequest(new { error = "CancelBetCancelled" })); } if (footballBet.ended) { return(BadRequest(new { error = "CancelBetEnded" })); } if (footballBet.dateLastBet < DateTime.Now) { return(BadRequest(new { error = "CancelBetLastBetPassed" })); } if (!checkBet(ref userFootballBet, order.userBet, footballBet, caller)) { return(BadRequest()); } try { ugCaller.coins += CheckBetType.calculateCancelRate(footballBet, userFootballBet.bet, _context); userFootballBet.valid = false; userFootballBet.dateInvalid = DateTime.Now; _context.Update(ugCaller); _context.Update(userFootballBet); _context.SaveChanges(); return(Ok(GroupPageManager.GetPage(caller, group, _context))); } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Kick a user out from the group /// </summary> /// <param name="order">The info to kick a user from the group</param> /// See <see cref="Areas.GroupManage.Models.KickUser"/> to know the param structure /// <returns>The updated group page</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public async System.Threading.Tasks.Task <IActionResult> removeUserAsync([FromBody] KickUser order) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to kick the user from the group if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } UserGroup targetUser = new UserGroup(); Group group = new Group(); if (!GroupAdminFuncionlities.checkFuncionality(user, ref group, order.groupName, ref targetUser, order.publicId, _context, GroupAdminFuncionality.REMOVE_USER, false)) { return(BadRequest()); } if (!group.open) { return(BadRequest(new { error = "GroupBanned" })); } try { _context.Entry(targetUser).Reference("User").Load(); User sendNew = targetUser.User; Guid targetUserid = targetUser.User.id; await QuitUserFromGroup.quitUser(targetUser, _context, _notificationHub); await KickChatNotification.sendKickMessageAsync(group.name, targetUser.User.publicid, _chatHub); InteractionManager.manageInteraction(targetUser.User, group, interactionType.KICKED, _context); using (var scope = _scopeFactory.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>(); group = dbContext.Group.Where(g => g.name == order.groupName).First(); User recv = dbContext.User.Where(u => u.id == targetUserid).First(); user = dbContext.User.Where(u => u.id == user.id).First(); Home.Util.GroupNew.launch(sendNew, group, null, Home.Models.TypeGroupNew.KICK_USER_USER, false, dbContext); Home.Util.GroupNew.launch(sendNew, group, null, Home.Models.TypeGroupNew.KICK_USER_GROUP, false, dbContext); await SendNotification.send(_notificationHub, group.name, recv, NotificationType.KICKED_GROUP, dbContext); return(Ok(GroupPageManager.GetPage(user, group, dbContext))); } } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Block/unblock a user from the group /// </summary> /// <param name="order">The info to block/unblock the user</param> /// See <see cref="Areas.GroupManage.Models.MakeAdmin_blockUser"/> to know the param structure /// <returns>The updated group page</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public async Task <IActionResult> blockUser([FromBody] MakeAdmin_blockUser order) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to kick the user from the group if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } UserGroup targetUser = new UserGroup(); Group group = new Group(); if (!GroupAdminFuncionlities.checkFuncionality(user, ref group, order.groupName, ref targetUser, order.publicId, _context, GroupAdminFuncionality.BLOCK_USER, order.make_unmake)) { return(BadRequest()); } if (!group.open) { return(BadRequest(new { error = "GroupBanned" })); } try { _context.Entry(user).Collection("groups").Load(); UserGroup callerUG = user.groups.Where(g => g.groupid == group.id).First(); targetUser.blocked = !targetUser.blocked; targetUser.role = RoleManager.getGroupNormal(_context); targetUser.blockedBy = callerUG.role; _context.Update(targetUser); _context.SaveChanges(); _context.Entry(targetUser).Reference("User").Load(); if (order.make_unmake) { await KickChatNotification.sendKickMessageAsync(group.name, targetUser.User.publicid, _chatHub); } await sendMessages(targetUser, group, order.make_unmake); return(Ok(GroupPageManager.GetPage(user, group, _context))); } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Change the weekly pay of a group /// </summary> /// <param name="order">The info to change the weekly pay</param> /// See <see cref="Areas.GroupManage.Models.ManageWeeklyPay"/> to know the param structure /// <returns>The updated group page</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public IActionResult manageWeeklyPay([FromBody] ManageWeeklyPay order) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to make admin to another user if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } Group group = new Group(); if (!GroupMakerFuncionlities.checkFuncionality(user, ref group, order.groupName, GroupMakerFuncionality.MANAGEWEEKPAY, _context, "", "")) { return(BadRequest()); } if (order.weeklyPay < 100 || order.weeklyPay > 2000) { return(BadRequest()); } if (!group.open) { return(BadRequest(new { error = "GroupBanned" })); } try { int oldPay = group.weeklyPay; group.weeklyPay = order.weeklyPay; _context.Update(group); _context.SaveChanges(); launchNews_changeUserCoins(group, oldPay, order.weeklyPay, user.id); _context.SaveChanges(); return(Ok(GroupPageManager.GetPage(user, group, _context))); } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Mange the password of the group /// </summary> /// <param name="order">The info to manage the password of the group</param> /// See <see cref="Areas.GroupManage.Models.ManagePassword"/> to know the param structure /// <returns>The updated group page</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public IActionResult managePassword([FromBody] ManagePassword order) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to make admin to another user if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } Group group = new Group(); if (!GroupMakerFuncionlities.checkFuncionality(user, ref group, order.name, GroupMakerFuncionality.MANAGE_PASSWORD, _context, order.newPassword, order.oldPassword)) { return(BadRequest()); } if (group.password != null && !PasswordHasher.areEquals(order.oldPassword, group.password)) { return(BadRequest(new { error = "IncorrectOldPassword" })); } if (!group.open) { return(BadRequest(new { error = "GroupBanned" })); } try { group.password = order.newPassword == null ? null : PasswordHasher.hashPassword(order.newPassword); _context.Update(group); _context.SaveChanges(); Home.Util.GroupNew.launch(null, group, null, Home.Models.TypeGroupNew.MAKE_PRIVATE, group.password != null, _context); return(Ok(GroupPageManager.GetPage(user, group, _context))); } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Gives the group-admin to a member of a group /// </summary> /// <param name="order">The infoto make a user admin of a group</param> /// See <see cref="Areas.GroupManage.Models.MakeAdmin_blockUser"/> to know the param structure /// <returns>The updated group page</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public async Task <IActionResult> makeAdmin([FromBody] MakeAdmin_blockUser order) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to make admin to another user if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } UserGroup targetUser = new UserGroup(); Group group = new Group(); if (!GroupAdminFuncionlities.checkFuncionality(user, ref group, order.groupName, ref targetUser, order.publicId, _context, GroupAdminFuncionality.MAKE_ADMIN, order.make_unmake)) { return(BadRequest()); } if (!group.open) { return(BadRequest(new { error = "GroupBanned" })); } try { targetUser.role = order.make_unmake ? RoleManager.getGroupAdmin(_context) : RoleManager.getNormalUser(_context); _context.Update(targetUser); _context.SaveChanges(); await sendNews(targetUser, group, order.make_unmake); return(Ok(GroupPageManager.GetPage(user, group, _context))); } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Get the page of the group /// </summary> /// <param name="groupName">The name of the group</param> /// <returns>The updated group page</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public IActionResult getGroupPage(string groupName) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } var groups = _context.Group.Where(g => g.name == groupName); // If the group doesn't exist if (groups.Count() != 1) { return(BadRequest()); } Group group = groups.First(); _context.Entry(group).Collection("users").Load(); // If the user doesn't belong to the group if (group.users.Where(u => u.userid == user.id && !u.blocked).Count() != 1) { return(BadRequest()); } if (!group.open) { return(BadRequest(new { error = "GroupBanned" })); } return(Ok(GroupPageManager.GetPage(user, group, _context))); }
/// <summary> /// Do a user fb on a fb /// </summary> /// <param name="order">The info to do the user fb</param> /// See <see cref="Areas.Bet.Models.DoFootballBet"/> to know the param structure /// <returns>IActionResult of the do user fb action</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public IActionResult doFootballBet([FromBody] DoFootballBet order) { User caller = TokenUserManager.getUserFromToken(HttpContext, _context); if (!caller.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(caller, _context)) { return(BadRequest("notAllowed")); } UserGroup ugCaller = new UserGroup(); Group group = new Group(); FootballBet fb = new FootballBet(); if (!UserInFootballBet.check(caller, ref group, order.groupName, ref ugCaller, ref fb, order.footballbet, _context)) { return(BadRequest()); } if (fb.cancelled) { return(BadRequest(new { error = "BetCancelled" })); } if (fb.ended) { return(BadRequest(new { error = "BetEnded" })); } if (fb.dateLastBet < DateTime.Now) { return(BadRequest(new { error = "BetLastBetPassed" })); } if (!checkBet(order.bet, ugCaller.coins, fb)) { return(BadRequest()); } if (!checkTypePriceWithBet(fb, order.homeGoals, order.awayGoals, order.winner)) { return(BadRequest()); } try { _context.Add(new UserFootballBet { FootballBet = fb, User = caller, bet = order.bet, winner = order.winner, homeGoals = order.homeGoals, awayGoals = order.awayGoals }); fb.usersJoined++; ugCaller.coins -= order.bet; _context.Update(ugCaller); _context.Update(fb); _context.SaveChanges(); return(Ok(GroupPageManager.GetPage(caller, group, _context))); } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Launchs a new fb /// </summary> /// <param name="order">The info to launch a new fb</param> /// See <see cref="Areas.Bet.Models.LaunchFootballBet"/> to know the param structure /// <returns>IActionResult of the launch fb action</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public IActionResult launchBet([FromBody] LaunchFootballBet order) { User caller = TokenUserManager.getUserFromToken(HttpContext, _context); if (!caller.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(caller, _context)) { return(BadRequest("notAllowed")); } Group group = new Group(); MatchDay match = new MatchDay(); TypeFootballBet typeBet = new TypeFootballBet(); TypePay typePay = new TypePay(); if (!GroupMakerFuncionlities.checkFuncionality(caller, ref group, order.groupName, GroupMakerFuncionality.STARTCREATE_FOOTBALL_BET, _context)) { return(BadRequest()); } if (!getMatchDay(ref match, order.matchday)) { return(BadRequest()); } if (!checkMaxBetAllowed(group)) { return(BadRequest()); } if (!checkParams(ref typeBet, order.typeBet, ref typePay, order.typePay)) { return(BadRequest()); } if (order.lastBetTime > match.date) { return(BadRequest()); } if (!checkMaxMin(order.minBet, order.maxBet)) { return(BadRequest()); } try { FootballBet fb = new FootballBet { MatchDay = match, Group = group, type = typeBet, typePay = typePay, minBet = order.minBet, maxBet = order.maxBet, winRate = typeBet.winRate + typePay.winRate, dateLastBet = order.lastBetTime, dateEnded = match.date }; _context.Add(fb); _context.SaveChanges(); launchNews(caller, group, fb); return(Ok(GroupPageManager.GetPage(caller, group, _context))); } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Cancel a fb /// </summary> /// <param name="betId">The id of the fb</param> /// <returns>IActionResult of the cancel action</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public IActionResult cancel([Required] string betId) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } List <FootballBet> bets = _context.FootballBets.Where(g => g.id.ToString() == betId).ToList(); if (bets.Count() != 1) { return(BadRequest()); } FootballBet bet = bets.First(); _context.Entry(bet).Reference("Group").Load(); Group group = bet.Group; if (!checkValidBet(bet)) { return(BadRequest()); } if (!checkAdmin(bet, user)) { return(BadRequest()); } if (bet.dateEnded < DateTime.Now) { return(BadRequest(new { error = "CantCancelTheFootballBet" })); } try { Home.Util.GroupNew.launch(null, group, bet, Home.Models.TypeGroupNew.FOOTBALLBET_CANCELLED_GROUP, false, _context); getMoneyBackAndLaunchNews(bet, group); bet.cancelled = true; bet.dateCancelled = DateTime.Now; _context.SaveChanges(); using (var scope = _scopeFactory.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>(); Group dbgroup = dbContext.Group.Where(g => g.name == group.name).First(); User dbUser = dbContext.User.Where(u => u.id == user.id).First(); return(Ok(GroupPageManager.GetPage(dbUser, dbgroup, dbContext))); } } catch (Exception) { return(StatusCode(500)); } }