Exemple #1
0
        public async Task <IActionResult> UpdateUser(int id, [FromBody] UserForUpdateDto userForUpdateDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != id)
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            if (userFromRepo == null)
            {
                return(NotFound($"Could not find user with id of {id}"));
            }

            _map.Map(userForUpdateDto, userFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {id} failed on save");
        }
Exemple #2
0
        public async Task <IActionResult> CreateInterest(int userId, int accountId, [FromBody] InterestForCreationDto interestForCreationDto)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != userId)
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            interestForCreationDto.UserId    = userId;
            interestForCreationDto.AccountId = accountId;

            var account = await _repo.GetAccount(accountId);

            if (account == null || account.UserId != userId)
            {
                return(Unauthorized());
            }
            var user = await _repo.GetUser(userId);

            // eens kijken of de mapper zo slim is dat ie de sender informatie ook invult in messageToReturn
            // zelfs bij de sender info die nu ietsanders heet.
            var interest = _map.Map <Interest>(interestForCreationDto);

            account.Percentage = interest.Percentage;

            _repo.Add(interest);
            if (await _repo.SaveAll())
            {
                var interestToReturn = _map.Map <InterestForDetailedDto>(interest);
                return(CreatedAtRoute("GetInterest", new { id = interest.Id }, interestToReturn));
            }
            throw new Exception("Creating the mutation failed on save");
        }
Exemple #3
0
        public async Task <IActionResult> CreateAccount(int userId, [FromBody] AccountForCreationDto accountForCreationDto)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != userId)
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            accountForCreationDto.UserId = userId;


            if (await _repo.GetAccount(userId, accountForCreationDto.Accountname) != null)
            {
                return(BadRequest("Account already exists"));
            }

            var user = await _repo.GetUser(accountForCreationDto.UserId);

            // eens kijken of de mapper zo slim is dat ie de sender informatie ook invult in messageToReturn
            // zelfs bij de sender info die nu ietsanders heet.
            var account = _map.Map <Account>(accountForCreationDto);

            account.Balance = 0;
            _repo.Add(account);
            if (await _repo.SaveAll())
            {
                var accountToReturn = _map.Map <AccountForDetailedDto>(account);
                return(CreatedAtRoute("GetAccount", new { id = account.Id }, accountToReturn));
            }
            throw new Exception("Creating the account failed on save");
        }
Exemple #4
0
        public async Task <IActionResult> CreateMutation(int userId, int accountId, [FromBody] MutationForCreationDto mutationForCreationDto)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != userId)
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            mutationForCreationDto.UserId    = userId;
            mutationForCreationDto.AccountId = accountId;

            var account = await _repo.GetAccount(accountId);

            if (account == null || account.UserId != userId)
            {
                return(Unauthorized());
            }
            var user = await _repo.GetUser(userId);

            // eens kijken of de mapper zo slim is dat ie de sender informatie ook invult in messageToReturn
            // zelfs bij de sender info die nu ietsanders heet.
            var mutation = _map.Map <Mutation>(mutationForCreationDto);

            var lastMutation = await _repo.GetMutation(account.LastMutationCreated, userId, accountId);

            if (lastMutation == null)
            {
                mutation.Balance = mutation.Amount;
                mutation.PrevId  = -account.Id;
            }
            else
            {
                mutation.PrevId  = lastMutation.Id;
                mutation.Balance = lastMutation.Balance + mutation.Amount;
                var diffdays = (mutation.InterestDate - lastMutation.InterestDate).TotalDays;
                int nrdays   = (int)diffdays;
                if (lastMutation.InterestDate.AddDays(diffdays).DayOfYear != mutation.InterestDate.DayOfYear)
                {
                    nrdays += 1;
                }
                // zoek eerst nog de gemiddelde rente op dus alle percentages af totdat
                var interest = (nrdays / 365) * account.Percentage;
                account.CalculatedInterest += interest;
            }
            if (mutation.Percentage != 0.0)
            {
                account.Percentage = mutation.Percentage;
            }
            else
            {
                mutation.Percentage = account.Percentage;
            }
            account.Balance             = mutation.Balance;
            account.LastMutationCreated = mutation.Created;
            account.LastMutation        = mutation;

            _repo.Add(mutation);
            if (await _repo.SaveAll())
            {
                var mutationToReturn = _map.Map <MutationForDetailedDto>(mutation);
                ScheduleTable.StartProcess(schedulePollerProcess, 5000);
                return(CreatedAtRoute("GetMutation", new { id = mutation.Id }, mutationToReturn));
            }
            throw new Exception("Creating the mutation failed on save");
        }