public async Task <Discount> CreateAsync(CreateDiscount createDiscount)
        {
            var discount = _mapper.Map <Discount>(createDiscount);

            _dbContext.Add(discount);
            await _dbContext.SaveChangesAsync();

            return(discount);
        }
 private Discount ToDomainModel(CreateDiscount model)
 {
     return(new Discount
     {
         DiscountId = model.Id,
         Type = model.Type,
         Name = model.Name,
         Percentage = model.Percentage
     });
 }
        public async Task <IActionResult> Create([FromBody] CreateDiscount Request)
        {
            try
            {
                if (Request == null)
                {
                    return(BadRequest(
                               new
                    {
                        error = "Your Request Cannot be Null"
                    }));
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(new
                    {
                        error = "Your Model state is not valid"
                    }));
                }

                var model = ToDomainModel(Request);
                var exist = await discountService.DiscountExist(model.DiscountId);

                if (!exist)
                {
                    var create = await discountService.AddDiscountAsync(model);

                    if (!create)
                    {
                        return(BadRequest(new { error = "Unable to create Discount" }));
                    }

                    var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
                    var locationUri = baseUrl + "/" + ApiRoutes.Discount.Get.Replace("{discountId}", model.DiscountId.ToString());
                    var response    = ToOutputModel(model);

                    return(Created(locationUri, response));
                }
                else
                {
                    return(Ok("Discount Type already exist"));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message + ":" + ex.StackTrace);
                throw;
            }
        }
        public async Task Create_Discount_Command_Should_Create_MongoEntity()
        {
            var customerId = Guid.NewGuid();
            await _mongoDbFixture.InsertAsync("Customers", new Customer(customerId, "*****@*****.**"));

            var command      = new CreateDiscount(Guid.NewGuid(), customerId, "DISCOUNT", 10);
            var creationTask = await _rabbitMqFixture.SubscribeAndGetAsync <DiscountCreated>(_mongoDbFixture.GetMongoEntity, command.Id);

            await _rabbitMqFixture.PublishAsync(command);

            var createdMongoEntity = await creationTask.Task;

            createdMongoEntity.Id.ShouldBe(command.Id);
            createdMongoEntity.CustomerId.ShouldBe(command.CustomerId);
            createdMongoEntity.Code.ShouldBe(command.Code);
            createdMongoEntity.Percentage.ShouldBe(command.Percentage);
        }
Esempio n. 5
0
        public async Task <ActionResult> Post(CreateDiscount command)
        {
            await _dispatcher.SendAsync(command.BindId(c => c.Id));

            return(Accepted());
        }
Esempio n. 6
0
 public async Task <IActionResult> Post(CreateDiscount command)
 => await SendAsync(command.BindId(c => c.Id), resourceId : command.Id,
                    resource : "discounts");
        public void TestCreateDiscount_Full()
        {
            var cd = new CreateDiscount
            {
                DiscountCode = "1",
                Name = "cheaper",
                Amount = 200,
                StartDate = new DateTime(2013, 9, 5),
                EndDate = new DateTime(2013, 9, 6)
            };

            string actual = cd.Serialize();
            const string expected = @"
            <discountCode>1</discountCode>
            <name>cheaper</name>
            <amount>200</amount>
            <startDate>2013-09-05</startDate>
            <endDate>2013-09-06</endDate>";
            Assert.AreEqual(expected, actual);
        }