public async Task <LabelModel> UpdateLabelAsync(LabelModel updatingLabel)
        {
            LabelModel currentLabel = await _labelsRepository.GetLabelByIdAsync(updatingLabel.Id);

            if (currentLabel == null)
            {
                throw new NotFoundException($"Label with id = {updatingLabel.Id} not found");
            }

            if (currentLabel.Version != updatingLabel.Version)
            {
                throw new VersionsNotMatchException();
            }

            var outboxMessage = OutboxMessageModel.Create(
                new LabelCreatedUpdatedMessage
            {
                LabelId  = updatingLabel.Id,
                OldTitle = currentLabel.Title,
                Title    = updatingLabel.Title,
                Color    = updatingLabel.Color
            }, Topics.Labels, MessageActions.Updated);

            return(await _labelsRepository.UpdateLabelAsync(updatingLabel, outboxMessage));
        }
        public async Task <LabelModel> CreateLabelAsync(LabelModel newLabel, string requestId)
        {
            if (!(await CheckAndSaveRequestIdAsync(requestId)))
            {
                throw new AlreadyHandledException();
            }

            try
            {
                newLabel.Init();

                var outboxMessage = OutboxMessageModel.Create(
                    new LabelCreatedUpdatedMessage
                {
                    LabelId = newLabel.Id,
                    Title   = newLabel.Title,
                    Color   = newLabel.Color
                }, Topics.Labels, MessageActions.Created);

                return(await _labelsRepository.CreateLabelAsync(newLabel, outboxMessage));
            }
            catch (Exception)
            {
                //rollback request id
                await _requestsRepository.DeleteRequestIdAsync(requestId);

                throw;
            }
        }
Example #3
0
        public async Task <LabelModel> CreateLabelAsync(LabelModel newLabel, OutboxMessageModel message)
        {
            string insertQuery = $"insert into {_tableName} (id, title, color, createddate, version) "
                                 + $"values('{newLabel.Id}', '{newLabel.Title}', '{newLabel.Color}', '{newLabel.CreatedDate}', {newLabel.Version});";

            string insertMessageQuery = ConstructInsertMessageQuery(message);

            insertQuery += insertMessageQuery;

            int res = await _connection.ExecuteAsync(insertQuery);

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

            return(await GetLabelByIdAsync(newLabel.Id));
        }
Example #4
0
        public async Task <LabelModel> UpdateLabelAsync(LabelModel updatedLabel, OutboxMessageModel message)
        {
            int newVersion = updatedLabel.Version + 1;

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

            string insertMessageQuery = ConstructInsertMessageQuery(message);

            updateQuery += insertMessageQuery;

            int res = await _connection.ExecuteAsync(updateQuery);

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

            return(await GetLabelByIdAsync(updatedLabel.Id));
        }