public async Task <ListProjectAggregate> CreateListAsync(ListModel newList, OutboxMessageModel message)
        {
            string insertQuery = $"insert into {_tableName} (id, title, projectId, createddate, version) "
                                 + $"values('{newList.Id}', '{newList.Title}', '{newList.ProjectId}', '{newList.CreatedDate}', {newList.Version});";

            string insertMessageQuery = ConstructInsertMessageQuery(message);

            insertQuery += insertMessageQuery;

            int res = await _connection.ExecuteAsync(insertQuery);

            if (res <= 0)
            {
                throw new DatabaseException("Create list failed");
            }

            return(await GetListByIdAsync(newList.Id));
        }
        public async Task <ListProjectAggregate> UpdateListAsync(ListModel updatedList, OutboxMessageModel message)
        {
            int newVersion = updatedList.Version + 1;

            string updateQuery = $"update {_tableName} set " +
                                 $"title = '{updatedList.Title}', " +
                                 $"version = {newVersion} " +
                                 $"where id = '{updatedList.Id}';";

            string insertMessageQuery = ConstructInsertMessageQuery(message);

            updateQuery += insertMessageQuery;

            int res = await _connection.ExecuteAsync(updateQuery);

            if (res <= 0)
            {
                throw new DatabaseException("Update list failed");
            }

            return(await GetListByIdAsync(updatedList.Id));
        }
        public async Task <ListProjectAggregate> UpdateListAsync(ListModel updatingList)
        {
            ListModel currentList = await _listsRepository.GetListByIdAsync(updatingList.Id);

            if (currentList == null)
            {
                throw new NotFoundException($"List with id = {updatingList.Id} not found");
            }

            if (currentList.Version != updatingList.Version)
            {
                throw new VersionsNotMatchException();
            }

            var outboxMessage = OutboxMessageModel.Create(
                new ListUpdatedMessage
            {
                ListId = updatingList.Id,
                Title  = updatingList.Title
            }, Topics.Lists, MessageActions.Updated);

            return(await _listsRepository.UpdateListAsync(updatingList, outboxMessage));
        }
        public async Task <ListProjectAggregate> CreateListAsync(ListModel newList, string requestId)
        {
            if (!(await CheckAndSaveRequestIdAsync(requestId)))
            {
                throw new AlreadyHandledException();
            }

            try
            {
                var project = await _projectsRepository.GetProjectAsync(newList.ProjectId);

                if (project == null)
                {
                    throw new NotFoundException($"Project with id {newList.ProjectId} not found");
                }

                newList.Init();

                var outboxMessage = OutboxMessageModel.Create(
                    new ListCreatedMessage
                {
                    ListId    = newList.Id,
                    ProjectId = newList.ProjectId,
                    Title     = newList.Title
                }, Topics.Lists, MessageActions.Created);

                return(await _listsRepository.CreateListAsync(newList, outboxMessage));
            }
            catch (Exception)
            {
                //rollback request id
                await _requestsRepository.DeleteRequestIdAsync(requestId);

                throw;
            }
        }
Example #5
0
        private async Task <string> PrepareListStageInternalAsync(MoveTaskPrepareListMessage message)
        {
            string transactionId = message.TransactionId;
            var    transaction   = await _transactionsRepository.GetTransactionAsync(transactionId);

            if (transaction != null && transaction.State == TransactionStates.Denied)
            {
                return($"Transaction {transactionId} already denied");
            }

            var moveTransaction = transaction == null
                ? MoveTaskTransaction.Create(transactionId, message.ProjectId, message.ListTitle)
                : MoveTaskTransaction.CreateFromBase(transaction);

            moveTransaction.State = TransactionStates.Processing;

            string projectId = message.ProjectId;
            var    project   = await _projectsRepository.GetProjectAsync(projectId);

            if (project == null)
            {
                string reason = $"Project {projectId} not found";

                var rollbackOutboxMessage = OutboxMessageModel.Create(
                    new RollbackTransactionMessage
                {
                    TransactionId = transactionId,
                    Reason        = reason
                }, Topics.Lists, TransactionMessageActions.MoveTask_Rollback);

                moveTransaction.State   = TransactionStates.Denied;
                moveTransaction.Message = reason;
                moveTransaction.UpdateData();

                await _transactionsRepository.CreateOrUpdateTransactionAsync(moveTransaction, rollbackOutboxMessage);

                return(reason);
            }

            try
            {
                var projectLists  = (await _listsRepository.GetProjectListsAsyn(projectId)).ToList();
                var sameTitleList = projectLists.FirstOrDefault(list => list.Title == message.ListTitle);

                if (sameTitleList == null)
                {
                    var newList = new ListModel
                    {
                        Title     = message.ListTitle,
                        ProjectId = projectId
                    };

                    newList.Init();

                    moveTransaction.ListId        = newList.Id;
                    moveTransaction.IsListCreated = true;

                    var completedOutboxMessage = OutboxMessageModel.Create(
                        new ListCreatedMessage
                    {
                        ListId    = newList.Id,
                        ProjectId = newList.ProjectId,
                        Title     = newList.Title
                    }, Topics.Lists, MessageActions.Created);

                    await _listsRepository.CreateListAsync(newList, completedOutboxMessage);
                }
                else
                {
                    moveTransaction.ListId        = sameTitleList.Id;
                    moveTransaction.IsListCreated = false;
                }

                moveTransaction.UpdateData();

                var outboxMessage = OutboxMessageModel.Create(
                    new MoveTaskListPreparedMessage
                {
                    TransactionId = transactionId,
                    ListId        = moveTransaction.ListId
                }, Topics.Lists, TransactionMessageActions.MoveTask_PrepareListCompleted);

                await _transactionsRepository.CreateOrUpdateTransactionAsync(moveTransaction, outboxMessage);

                return("Prepare list stage completed successful");
            }
            catch (Exception e)
            {
                string errorReason = $"Unknonw exception occured:\n{e.Message}\n{e.StackTrace}";
                moveTransaction.Message = errorReason;
                moveTransaction.State   = TransactionStates.Denied;

                var failedOutboxMessage = OutboxMessageModel.Create(
                    new RollbackTransactionMessage
                {
                    TransactionId = transactionId,
                    Reason        = moveTransaction.Message
                }, Topics.Lists, TransactionMessageActions.MoveTask_Rollback);

                await _transactionsRepository.CreateOrUpdateTransactionAsync(moveTransaction, failedOutboxMessage);

                throw;
            }
        }