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));
        }
Exemple #2
0
        public async Task <ActionResult <ActionsModels> > CreateAsync([FromBody] InsertActionsResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var result = await _ActionsService.CreateAsync(resource);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }


            return(Ok(new { msg = "新增成功" }));
        }
        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)));
        }