public async Task <IActionResult> CreatePaymentForFlat(int flatId, [FromBody] PaymentDTO paymentDTO, [FromHeader] List <int> userIds)
        {
            Payment payment = _mapper.Map <PaymentDTO, Payment>(paymentDTO);

            await _paymentsRepository.Add(payment);

            List <Payment> payments = await GetAllPaymentsFromFlatId(flatId);

            Flat flat = await _flatRepository.Get(flatId);

            payments.Add(payment);

            flat.Payments = payments;

            await _flatRepository.Update(flat);

            foreach (int userId in userIds)
            {
                User user = await _userRepository.Get(userId);

                UserPayment userPayment = new UserPayment {
                    Payment = payment, User = user, PaymentId = payment.Id, UserId = user.Id
                };

                user.UserPayments.Add(userPayment);

                await _userRepository.Update(user);
            }

            return(Ok(paymentDTO));
        }
Beispiel #2
0
        public async Task <IActionResult> CreateChoreForFlat([FromBody] ChoreDTO choreDTO)
        {
            ClaimsIdentity identity = HttpContext.User.Identity as ClaimsIdentity;

            int userID = Int16.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value);
            var usr    = await _userRepository.Get(userID);

            int?flatId   = usr.FlatId;
            var assignee = await _userRepository.Get(choreDTO.Assignee);

            if (assignee == null)
            {
                return(BadRequest());
            }

            Chore chore = _mapper.Map <ChoreDTO, Chore>(choreDTO);

            chore.AssignedUser = assignee;

            await _choresRepository.Add(chore);

            List <Chore> chores = await GetAllChoresFromFlatId(flatId);

            Flat flat = await _flatRepository.Get(flatId.Value);

            chores.Add(chore);
            flat.Chores = chores;
            await _flatRepository.Update(flat);

            return(Ok());
        }
Beispiel #3
0
        public async Task <IActionResult> CreatePaymentForFlat([FromBody] PaymentDTO paymentDTO, [FromHeader] List <int> userIds)
        {
            ClaimsIdentity identity = HttpContext.User.Identity as ClaimsIdentity;

            int userID = Int16.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value);

            var usr = await _userRepository.Get(userID);

            int?flatId = usr.FlatId;

            Payment payment = _mapper.Map <PaymentDTO, Payment>(paymentDTO);

            await _paymentsRepository.Add(payment);

            List <Payment> payments = await GetAllPaymentsFromFlatId(flatId);

            Flat flat = await _flatRepository.Get(flatId.Value);

            payments.Add(payment);

            flat.Payments = payments;

            await _flatRepository.Update(flat);

            foreach (int userId in userIds)
            {
                User user = await _userRepository.Get(userId);

                UserPayment userPayment = new UserPayment {
                    Payment = payment, User = user, PaymentId = payment.Id, UserId = user.Id
                };

                user.UserPayments.Add(userPayment);

                await _userRepository.Update(user);
            }

            return(Ok(paymentDTO));
        }
        public async Task TestGetChoresForFlatAsync()
        {
            var chore = new Chore
            {
                Title        = "lawn",
                Description  = "mow the lawn",
                AssignedUser = await _userRepository.Get(1),
                DueDate      = new DateTime(2021, 05, 05),
                Completed    = false,
                Recurring    = true,
            };

            Chore result = await _choresRepository.Add(chore);

            // We need add the chore to the flat belonging to the active user
            int  userId     = 1; // as initialised in HttpContextHelper
            User activeUser = await _userRepository.Get(userId);

            Flat userFlat = activeUser.Flat;

            userFlat.Chores.Add(result);
            await _flatRepository.Update(userFlat);

            // Act
            var response = await _choreController.GetAllChoresForFlat();

            // Assert
            Assert.IsInstanceOf <OkObjectResult>(response);
            OkObjectResult actionResult = (OkObjectResult)response;

            Assert.IsInstanceOf <List <ChoreDTO> >(actionResult.Value);
            List <ChoreDTO> actualChores = (List <ChoreDTO>)actionResult.Value;

            List <ChoreDTO> expectedChores = new List <ChoreDTO> {
                new ChoreDTO
                {
                    Id          = 1,
                    Title       = "dishes",
                    Description = "do the dishes",
                    Assignee    = 1,
                    DueDate     = new DateTime(2020, 04, 04),
                    Completed   = false,
                    Recurring   = true
                }, new ChoreDTO
                {
                    Id          = 2,
                    Title       = "rubbish",
                    Description = "take out the rubbish",
                    Assignee    = 3,
                    DueDate     = new DateTime(2020, 05, 05),
                    Completed   = false,
                    Recurring   = true
                }, new ChoreDTO
                {
                    Id          = 3,
                    Title       = "lawn",
                    Description = "mow the lawn",
                    Assignee    = 1,
                    DueDate     = new DateTime(2021, 05, 05),
                    Completed   = false,
                    Recurring   = true,
                }
            };

            Assert.AreEqual(expectedChores, actualChores);
        }