Example #1
0
        public async Task <IActionResult> GetAccount(int id, int userId)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

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

            var user = await _repo.GetUser(userId);

            var account = await _repo.GetAccount(id);

            if (account == null || account.UserId != userId)
            {
                return(Unauthorized());
            }

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

            if (lastMutation != null)
            {
                account.LastMutation = lastMutation;
                _map.Map <MutationForDetailedDto>(lastMutation);
            }
            var accountToReturn = _map.Map <AccountForDetailedDto>(account);

            return(Ok(accountToReturn));
        }
Example #2
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");
        }