コード例 #1
0
        public async Task <ResultOrError <AuthenticationResult> > Login(string emailAddressOrUserName, string password)
        {
            var user = await _context.Users
                       .Include(x => x.RefreshToken)
                       .SingleOrDefaultAsync(x =>
                                             x.EmailAddress.ToLower() == emailAddressOrUserName.ToLower() ||
                                             x.UserName.ToLower() == emailAddressOrUserName.ToLower());

            if (user == null || !new SimpleHash().Verify(password, user.Password))
            {
                return(IdentityErrors.InvalidUser);
            }

            if (user.RefreshToken != null)
            {
                _context.Remove(user.RefreshToken);
            }

            var jwt = GenerateJwtToken(user);

            var refreshToken = GenerateRefreshToken(user.Id);
            await _context.RefreshTokens.AddAsync(refreshToken);

            await _context.SaveChangesAsync();

            await _setupNewUser.Handle(user.Id);

            return(new AuthenticationResult
            {
                AccessToken = jwt.AccessToken,
                RefreshToken = refreshToken.Token,
                ExpiresAt = jwt.ExpiresAt,
            });
        }
コード例 #2
0
        public async Task <IActionResult> PutInvoice(int id, Invoice invoice)
        {
            if (id != invoice.InvoiceID)
            {
                return(BadRequest());
            }

            _context.Entry(invoice).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InvoiceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> PutCompany(int id, Company company)
        {
            if (id != company.CompanyID)
            {
                return(BadRequest());
            }

            _context.Entry(company).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CompanyExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #4
0
        public async Task <IActionResult> PutAddress(int id, Address address)
        {
            if (id != address.AddressID)
            {
                return(BadRequest());
            }

            _context.Entry(address).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AddressExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #5
0
        private async Task <Guid> Create(SaveExpenseAggregateParameters parameters)
        {
            var aggregate = ExpensesAggregate.Create(parameters.UserId, parameters.Name, parameters.AddedDate, parameters.IsDraft);
            await _context.ExpensesAggregates.AddAsync(aggregate);

            await _context.SaveChangesAsync();

            return(aggregate.Id);
        }
コード例 #6
0
        public async Task <SuccessOrError> Handle(Guid userId)
        {
            var user = await _context.Users.SingleOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                return(CommonErrors.UserNotExist);
            }

            if (user.WasSetup)
            {
                return(SuccessOrError.FromSuccess());
            }

            await InitializeDefaultExpenseTypes(user.Id);

            user.Setup();
            await _context.SaveChangesAsync();

            return(SuccessOrError.FromSuccess());
        }
コード例 #7
0
        public async Task <ResultOrError <ExpenseTypeModel> > Handle(CreateExpenseTypeParameters parameters)
        {
            if (!await _context.DoesExist <User>(parameters.UserId))
            {
                return(CommonErrors.UserNotExist);
            }

            var expenseTypeExist = await _context.ExpenseTypes.AnyAsync(
                x => x.Name.ToLower() == parameters.Name.ToLower() &&
                x.UserId == parameters.UserId);

            if (expenseTypeExist)
            {
                return(ExpenseTypeAlreadyExist);
            }

            var type = ExpenseType.Create(parameters.UserId, parameters.Name);
            await _context.ExpenseTypes.AddAsync(type);

            await _context.SaveChangesAsync();

            return(type.ToModel());
        }
コード例 #8
0
        public async Task <ResultOrError <ExpenseBillFileModel> > Handle(AddBillFileParameters parameters)
        {
            if (parameters.File == null || parameters.File.Length == 0)
            {
                return(BillFileIsEmpty);
            }

            if (parameters.File.Length > MaximumBillFileSizeInMB * 1000000)
            {
                return(BillFileIsTooBig);
            }

            var aggregate = await _context.ExpensesAggregates
                            .Include(x => x.ExpenseBillFiles)
                            .SingleOrDefaultAsync(x => x.Id == parameters.AggregateId && x.UserId == parameters.UserId);

            if (aggregate == null)
            {
                return(CommonErrors.ExpenseAggregateNotFound);
            }

            var fileName = $"{aggregate.ExpenseBillFiles.Count()}-{parameters.FileName}";
            var fileUri  = await _billFileStorage.Upload(
                new UploadBillBlob(
                    file : parameters.File,
                    userId : parameters.UserId,
                    aggregateId : parameters.AggregateId,
                    fileName : fileName));

            var entity = ExpenseBillFile.Create(parameters.AggregateId, fileName, fileUri);

            await _context.ExpenseBills.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(entity.ToModel());
        }
コード例 #9
0
        public async Task <ResultOrError <ExpenseModel> > Handle(AddExpenseParameters input)
        {
            if (!await _context.DoesExist <User>(input.UserId))
            {
                return(CommonErrors.UserNotExist);
            }

            if (input.ExpenseTypeId.HasValue && !await _context.DoesExist <ExpenseType>(input.ExpenseTypeId.Value))
            {
                return(ExpenseTypeNotExist);
            }

            Expense newExpense;
            var     aggregateId = input.AggregateId;

            if (aggregateId.HasValue)
            {
                if (!await _context.ExpensesAggregates.AnyAsync(x => x.Id == input.AggregateId.Value))
                {
                    return(CommonErrors.ExpenseAggregateNotFound);
                }
            }
            else
            {
                var aggregate = ExpensesAggregate.Create(input.UserId, input.Name, input.AddedDate, isDraft: false);
                await _context.ExpensesAggregates.AddAsync(aggregate);

                aggregateId = aggregate.Id;
            }

            newExpense = Expense.Create(input.Name, input.Amount, aggregateId.Value, input.ExpenseTypeId);
            await _context.Expenses.AddAsync(newExpense);

            await _context.SaveChangesAsync();

            return(newExpense.ToModel());
        }
コード例 #10
0
 public async Task <int> Commit()
 {
     return(await _dbContext.SaveChangesAsync());
 }