public async Task <IActionResult> PostArticleAsync([FromBody] DTO.ArticleInput article)
        {
            var userName           = User.FindFirst("userName")?.Value;
            var normalizedUserName = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var insertedArticle = await ArticleManager.InsertEntityAsync(article, userName, normalizedUserName);

            return(Ok(new DTO.SuccessResponse <DTO.ArticleOutput>
            {
                Success = true,
                Content = insertedArticle
            }));
        }
Ejemplo n.º 2
0
        public async Task <DTO.ArticleOutput> InsertEntityAsync(DTO.ArticleInput entity, string userName,
                                                                string normalizedUserName)
        {
            try
            {
                await TransactionHandler.BeginTransactionAsync();

                var person = new DAO.Person
                {
                    UserName           = userName,
                    NormalizedUserName = normalizedUserName
                };
                var managedPerson = await PersonRepository.InsertOrGetEntityAsync(person);

                var convertedUnit = Mapper.Map <DAO.Unit>(entity.Unit);
                var managedUnit   = await UnitRepository.InsertOrGetEntityAsync(convertedUnit);

                var currentDateTime = DateTime.UtcNow;
                var article         = new DAO.Article
                {
                    Person         = managedPerson,
                    Unit           = managedUnit,
                    Name           = entity.Name,
                    Description    = entity.Description,
                    Price          = entity.Price,
                    Amount         = entity.Amount,
                    Size           = entity.Size,
                    CreatedAt      = currentDateTime,
                    UpdatedAt      = currentDateTime,
                    ExpirationDate = entity.ExpirationDate
                };
                var managedArticle = await ArticleRepository.InsertEntityAsync(article);

                await TransactionHandler.CommitTransactionAsync();

                return(Mapper.Map <DTO.ArticleOutput>(managedArticle));
            }
            catch
            {
                await TransactionHandler.RollbackTransactionAsync();

                throw;
            }
            finally
            {
                await TransactionHandler.DisposeTransactionAsync();
            }
        }
Ejemplo n.º 3
0
        public async Task <DTO.ArticleOutput> UpdateEntityAsync(long id, DTO.ArticleInput entity, string userName,
                                                                string normalizedUserName)
        {
            try
            {
                await TransactionHandler.BeginTransactionAsync();

                var foundArticle = await CheckAccessRightsAndGetArticleAsync(id, userName, normalizedUserName);

                var convertedUnit = Mapper.Map <DAO.Unit>(entity.Unit);
                var managedUnit   = await UnitRepository.InsertOrGetEntityAsync(convertedUnit);

                foundArticle.Unit           = managedUnit;
                foundArticle.Name           = entity.Name;
                foundArticle.Description    = entity.Description;
                foundArticle.Price          = entity.Price;
                foundArticle.Amount         = entity.Amount;
                foundArticle.Size           = entity.Size;
                foundArticle.UpdatedAt      = DateTime.UtcNow;
                foundArticle.ExpirationDate = entity.ExpirationDate;
                await ArticleRepository.UpdateEntityAsync(foundArticle);

                await TransactionHandler.CommitTransactionAsync();

                return(Mapper.Map <DTO.ArticleOutput>(foundArticle));
            }
            catch
            {
                await TransactionHandler.RollbackTransactionAsync();

                throw;
            }
            finally
            {
                await TransactionHandler.DisposeTransactionAsync();
            }
        }