public async Task <ServiceResult <int> > Get(ClaimsPrincipal user, int groupId, int actionId) { var group = groupsRepository.Get(groupId); if (group is null) { return(ServiceResult <int> .Error(404, "Group was not found")); } var authorizationResult = await authorizationService.AuthorizeAsync(user, group, GroupOperations.Write); if (!authorizationResult.Succeeded) { return(ServiceResult <int> .Error(401, "Unauthorized")); } var action = actionsRepository.Get(actionId); if (action is null || action.GroupId != groupId) { return(ServiceResult <int> .Error(404, "Action was not found")); } return(ServiceResult <int> .Success(ActionViewModel.FromModel(action))); }
public async Task <ServiceResult <int> > AddUser(string groupCode, User user) { var group = groupsRepository.Get(groupCode); if (group is null) { return(ServiceResult <int> .Error(404, "Group does not exist")); } if (groupUsersRepository.GetCount(group.Id, user.Id) != 0) { return(ServiceResult <int> .Error(400, "User is already in group")); } var groupUser = new GroupUser() { GroupId = group.Id, UserId = user.Id }; var result = await groupUsersRepository.CreateAsync(groupUser); if (result is null) { return(ServiceResult <int> .Error(500, "Error adding user to group")); } await actionService.CreateAsync(group.Id, user.Id, "Joined", $"{user.UserName} has joined the group"); return(ServiceResult <int> .Success((IViewModel <int>) null)); }
public async Task <ServiceResult <int> > DeleteAsync(ClaimsPrincipal user, int id) { var group = groupsRepository.Get(id); if (group is null) { return(ServiceResult <int> .Error(404, "Group not found")); } var authorizationResult = await authorizationService.AuthorizeAsync(user, group, GroupOperations.Write); if (!authorizationResult.Succeeded) { return(ServiceResult <int> .Error(401, "Unauthorized")); } group.Deleted = true; var result = await groupsRepository.UpdateAsync(group); if (result is null) { return(ServiceResult <int> .Error(500, "Error deleting group")); } return(ServiceResult <int> .Success(GroupViewModel.FromModel(result))); }
public async Task <IActionResult> GetGroupParticipants(string groupGuid, int?page, int?pageSize) { var creator = await _groupsRepository.Get(groupGuid); return(new OkObjectResult(new { Users = (await _groupsRepository.GetGroupUsers(groupGuid, page, pageSize)) .Select(user => new UserFoundDto(user)), CreatorGuid = creator.Creator.UserGuid })); }
public void DeleteGroup(Group selectedGroup) { using (var scope = Db.BeginWork()) { var baseGroup = _groupsRepository.Get(u => u.Id == selectedGroup.Id); if (baseGroup != null) { _groupsRepository.Delete(baseGroup); scope.SaveChanges(); } } }
public async Task <ServiceResult <int> > CreateAsync(int groupId, ClaimsPrincipal user, TransactionRequestModel transaction) { var group = groupsRepository.Get(groupId); if (group is null) { return(ServiceResult <int> .Error(404, "Group was not found")); } var authorizationResult = await authorizationService.AuthorizeAsync(user, group, GroupOperations.Read); if (!authorizationResult.Succeeded) { return(ServiceResult <int> .Error(401, "Unauthorized")); } var authenticatedUser = await usersManager.GetUserAsync(user); var transactionEntity = new Transaction() { Amount = transaction.Amount, Description = transaction.Description, CategoryId = transaction.CategoryId, UserId = authenticatedUser.Id, GroupId = groupId }; var result = await transactionsRepository.CreateAsync(transactionEntity); if (result is null) { return(ServiceResult <int> .Error(500, "Could not create transaction")); } await actionService.CreateAsync(group.Id, authenticatedUser.Id, "Transaction", $"{authenticatedUser.UserName} has added transaction"); return(ServiceResult <int> .Success(TransactionViewModel.FromModel(result))); }
public async Task <ServiceResult <int> > CreateAsync(ClaimsPrincipal user, int groupId, CategoryRequestModel category) { var group = groupsRepository.Get(groupId); if (group is null) { return(ServiceResult <int> .Error(404, "Group was not found")); } var authorizationResult = await authorizationService.AuthorizeAsync(user, group, GroupOperations.Write); if (!authorizationResult.Succeeded) { return(ServiceResult <int> .Error(401, "Unauthorized")); } var existing = categoriesRepository.GetCount(groupId, category.Name); if (existing != 0) { return(ServiceResult <int> .Error(400, "Categgory with this name already exists in this group")); } var categoryEntity = new Category() { Name = category.Name, Description = category.Description, GroupId = groupId }; var result = await categoriesRepository.CreateAsync(categoryEntity); if (result is null) { return(ServiceResult <int> .Error(500, "Could not create category")); } return(ServiceResult <int> .Success(CategoryViewModel.FromModel(result))); }
public async Task <ServiceResult <string> > GetStat(ClaimsPrincipal user, int groupId, string stat) { var group = groupsRepository.Get(groupId); if (group is null) { return(ServiceResult <string> .Error(404, "Group was not found")); } var authorizationResult = await authorizationService.AuthorizeAsync(user, group, GroupOperations.Write); if (!authorizationResult.Succeeded) { return(ServiceResult <string> .Error(401, "Only group admin can view statistics")); } switch (stat.ToLower()) { case "average": return(ServiceResult <string> .Success(repository.GetAverageTransaction(groupId))); case "biggestspender": return(ServiceResult <string> .Success(repository.GetBiggestSpender(groupId))); case "smallestspender": return(ServiceResult <string> .Success(repository.GetSmallestSpender(groupId))); case "biggestdonator": return(ServiceResult <string> .Success(repository.GetBiggestDonator(groupId))); case "smallestdonator": return(ServiceResult <string> .Success(repository.GetSmallestDonator(groupId))); case "users": return(ServiceResult <string> .Success(repository.GetSpendingByUsers(groupId))); case "categories": return(ServiceResult <string> .Success(repository.GetSpendingByCategories(groupId))); default: return(ServiceResult <string> .Error(404, "stat was not found")); } }
public async Task <Message> SendMessage(string text, string groupGuid, string userGuid, IClientProxy clients) { var sender = await _users.Get(userGuid); if (sender == null) { throw new ArgumentException("Login not found"); } var group = await _groups.Get(groupGuid); if (group == null) { throw new ArgumentException("Group not found"); } var participants = await _groups.GetGroupUsers(groupGuid); if (participants.Count(user => user.UserGuid == sender.UserGuid) < 1) { throw new ArgumentException("User is not in a group"); } var message = new Message() { Group = group, Sender = sender, Text = text, Time = DateTime.Now }; var m = await _messages.Create(message); var incoming = new GroupMessageDto(message); var users = await _groups.GetGroupUsers(groupGuid); foreach (var user in users) { await _pusher.ChatMessage(incoming, user, group, clients); } return(message); }
public async Task <HubResponse> InitiateCall(InitiateCallRequestDto initiateCallRequest) { _logger.Trace($"InitiateCall reqest: {JsonConvert.SerializeObject(initiateCallRequest)}"); try { var answer = await _janus.InitiateCall(initiateCallRequest.GroupGuid, initiateCallRequest.Offer); var user = await _users.Get(Context.User.Identity.Name); var group = await _groups.Get(initiateCallRequest.GroupGuid); await _pusher.CallStarted(user, group, Clients.All); await _users.UpdateInCall(user, group); await _groups.UpdateInCall(group, true); return(new HubSuccessResponse(answer)); } catch (Exception ex) { return(new HubErrorResponse(1, ex.Message)); } }
public async Task <GroupViewModel> GetGroupByIdAsync(int id) { var group = await _repository.Get(id); return(MapGroupToGroupViewModel(group)); }
public IEnumerable <Group> Get() { return(_groupsRepository.Get()); }
public Group Get([FromRoute] long id) { return(repository.Get(id)); }
// GET: GroupsController/Edit/5 public ActionResult Edit(int id) { return(View(_groupsRepository.Get(id))); }