public void CreateBill_ValidValuesPassed_ReturnsCreatedAtRouteResult()
        {
            BillCreateDto bill = new BillCreateDto()
            {
                ClientId = 100, category = "SEWER", period = 202111, Amount = 200
            };
            var response = controller.CreateBill(bill);

            Assert.IsType <CreatedAtRouteResult>(response.Result);
        }
        public void CreateBill_ExistingValuesPassed_ReturnsBadRequestResult()
        {
            BillCreateDto bill = new BillCreateDto()
            {
                ClientId = 100, category = "ELECTRICITY", period = 202102, Amount = 200
            };
            var response = controller.CreateBill(bill);

            Assert.IsType <BadRequestResult>(response.Result);
        }
        public void CreateBill_MalformedPeriodPassed_ReturnsBadRequestResult()
        {
            BillCreateDto bill = new BillCreateDto()
            {
                ClientId = 100, category = "SEWER", period = 15, Amount = 200
            };
            var response = controller.CreateBill(bill);

            Assert.IsType <BadRequestResult>(response.Result);
        }
        public void CreateBill_InvalidClientIdPassed_ReturnsBadRequestResult()
        {
            BillCreateDto bill = new BillCreateDto()
            {
                ClientId = 555, category = "WATER", period = 202002, Amount = 200
            };
            var response = controller.CreateBill(bill);

            Assert.IsType <BadRequestResult>(response.Result);
        }
Example #5
0
        public ActionResult <BillReadDto> CreateBill(BillCreateDto billCreateDto)
        {
            var serviceEntity = repository.GetServiceByShortname(billCreateDto.category);

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

            var clientEntity = repository.GetClientById(billCreateDto.ClientId);

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

            int period = billCreateDto.period;

            var regex = @"^\d{4}(0[1-9]|1[0-2])$";
            var match = Regex.Match(period.ToString(), regex, RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                return(BadRequest());
            }

            decimal amount = billCreateDto.Amount;

            if (amount <= 0)
            {
                return(BadRequest());
            }

            var billEntity = repository.GetBillByClientServiceAndPeriod(clientEntity, serviceEntity, period);

            if (billEntity != null)
            {
                return(BadRequest());
            }

            Bill bill = new Bill();

            bill.Client  = clientEntity;
            bill.Service = serviceEntity;
            bill.Period  = period;
            bill.Status  = BillingStatus.Pending;
            bill.Amount  = amount;

            repository.CreateBill(bill);
            repository.SaveChanges();

            var billReadDto = mapper.Map <BillReadDto>(bill);

            return(CreatedAtRoute(nameof(GetBillById), new { Id = billReadDto.Id }, billReadDto));
        }
Example #6
0
        public async Task <IActionResult> CreateBill([FromBody] BillCreateDto dto)
        {
            var bill = await this.sender.Send(new CreateBillCommand(
                                                  this.currentUserId,
                                                  dto.BankAccountId,
                                                  dto.ShopName,
                                                  dto.Price,
                                                  dto.Category.FromDto(),
                                                  dto.Date,
                                                  dto.Notes));

            if (bill.Status != ResultStatus.Success)
            {
                return(this.ToActionResult(bill));
            }

            return(Created($"{HttpContext.Request.Path}/{bill.Value.Id}", bill.Value));
        }