コード例 #1
0
        public async Task <ServiceResult <int> > UpdateAsync(ClaimsPrincipal user, int groupId, int transactionId, 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.Write);

            if (!authorizationResult.Succeeded)
            {
                return(ServiceResult <int> .Error(401, "Unauthorized"));
            }

            var transactionEntity = transactionsRepository.Get(transactionId);

            if (transactionEntity is null || transactionEntity.GroupId != groupId)
            {
                return(ServiceResult <int> .Error(404, "Transaction was not found"));
            }

            transactionEntity.Amount      = transaction.Amount;
            transactionEntity.Description = transaction.Description;
            transactionEntity.CategoryId  = transaction.CategoryId;

            var result = await transactionsRepository.UpdateAsync(transactionEntity);

            if (result is null)
            {
                return(ServiceResult <int> .Error(500, "Could not update transaction"));
            }
            return(ServiceResult <int> .Success(TransactionViewModel.FromModel(result)));
        }
コード例 #2
0
        public async Task <ServiceResult <int> > DeleteAsync(ClaimsPrincipal user, int groupId, int transactionId)
        {
            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 transaction = transactionsRepository.Get(transactionId);

            if (transaction is null || transaction.GroupId != groupId)
            {
                return(ServiceResult <int> .Error(404, "Transaction was not found"));
            }

            transaction.Deleted = true;

            var result = await transactionsRepository.UpdateAsync(transaction);

            if (result is null)
            {
                return(ServiceResult <int> .Error(500, "Error deleting transaction"));
            }
            return(ServiceResult <int> .Success(TransactionViewModel.FromModel(result)));
        }
コード例 #3
0
        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)));
        }
コード例 #4
0
        public async Task <ServiceResult <int> > Get(ClaimsPrincipal user, int groupId, int transactionId)
        {
            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 transaction = transactionsRepository.Get(transactionId);

            if (transaction is null || transaction.GroupId != groupId)
            {
                return(ServiceResult <int> .Error(404, "Transaction was not found"));
            }
            return(ServiceResult <int> .Success(TransactionViewModel.FromModel(transaction)));
        }