Exemple #1
0
        public async Task <AbstractAnswer <Transaction> > Handle(CreateTransactionCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var tempId = Guid.NewGuid();

                var foundedBill =
                    await context.Bills.FirstOrDefaultAsync(x => x.Id == request.BillId, cancellationToken);

                if (foundedBill is null)
                {
                    return(CreateFailed(new[] { "Bill not found" }));
                }

                var foundedType =
                    await context.TransactionTypes.FirstOrDefaultAsync(x => x.Id == request.TypeId, cancellationToken);

                if (foundedType is null)
                {
                    return(CreateFailed(new[] { "Transactions type not found" }));
                }

                context.Transactions.Add(new TransactionDB
                {
                    Id          = tempId,
                    BillId      = request.BillId,
                    TypeId      = request.TypeId,
                    Date        = request.Date,
                    Description = request.Description,
                    Value       = request.Value,
                });

                await context.SaveChangesAsync(cancellationToken);

                return(CreateSuccess(new Transaction
                {
                    Id = tempId,
                    Type = new TransactionType
                    {
                        Id = request.TypeId,
                        Title = foundedType.Title
                    },
                    Bill = new Bill
                    {
                        Id = request.BillId,
                        Title = foundedBill.Title,
                    },
                    Info = new TransactionInfo
                    {
                        Date = request.Date,
                        Description = request.Description,
                        Value = request.Value,
                    },
                }));
            }
            catch
            {
                return(CreateFailed(new[] { "Database error" }));
            }
        }
Exemple #2
0
        public async Task <AbstractAnswer <Bill> > Handle(CreateBillCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var tempId = Guid.NewGuid();

                var foundedUser = await context.Users
                                  .FirstOrDefaultAsync(x => x.Id == request.UserId, cancellationToken);

                if (foundedUser is null)
                {
                    return(CreateFailed(new[] { "User doesn't exists" }));
                }

                context.Bills.Add(new BillDB
                {
                    Id     = tempId,
                    Title  = request.Title,
                    UserId = request.UserId,
                });

                await context.SaveChangesAsync(cancellationToken);

                return(CreateSuccess(new Bill
                {
                    Id = tempId,
                    Title = request.Title,
                }));
            }
            catch
            {
                return(CreateFailed(new[] { "Database error" }));
            }
        }
Exemple #3
0
        public async Task <Unit> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            context.Users.Add(new UserDB
            {
                Id       = request.Id,
                Username = request.Username,
            });

            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }