public async Task CreateAsyncReturnsSuccessOnSuccess()
        {
            //Setup
            var request = new CreateHospitalRequest
            {
                Name    = "Name",
                Address = "Address"
            };

            _hospitalRepository
            .Setup(x => x.GetByName(It.IsAny <string>()))
            .Returns((Hospital)null);

            _hospitalRepository
            .Setup(x => x.Add(It.IsAny <Hospital>()))
            .Returns(new Hospital
            {
                Id      = Guid.NewGuid(),
                Name    = request.Name,
                Address = request.Address
            });

            var service = new HospitalDomainService(_dbContext.Object);

            // Execute
            var response = await service.CreateAsync(request);

            // Assert
            Assert.AreEqual(response.Result, CreateHospitalResult.Success);
            Assert.AreEqual(response.Entity.Name, request.Name);
            Assert.AreEqual(response.Entity.Address, request.Address);
            _hospitalRepository.Verify(x => x.Add(It.Is <Hospital>(h => h.Name.Equals(request.Name) && h.Address.Equals(request.Address))), Times.Once);
            _dbContext.Verify(x => x.SaveChangesAsync(), Times.Once);
        }
Beispiel #2
0
        public async Task <JsonResult> PostAsync(CreateHospitalRequest created)
        {
            created.UserId = UserId;
            var data = await _mediator.ToPipeAsync <CreateHospitalRequest, Hospital>(created);

            return(Json(data));
        }
        public async Task CreateAsyncReturnsAlreadyExistsIfNameExists()
        {
            //Setup
            var request = new CreateHospitalRequest
            {
                Name    = "Name",
                Address = "Address"
            };

            _hospitalRepository
            .Setup(x => x.GetByName(It.IsAny <string>()))
            .Returns(new Hospital {
                Name = request.Name, Address = request.Address
            });

            var service = new HospitalDomainService(_dbContext.Object);

            // Execute
            var response = await service.CreateAsync(request);

            // Assert
            Assert.AreEqual(response.Result, CreateHospitalResult.AlreadyExists);
            Assert.IsNull(response.Entity);
            _hospitalRepository.Verify(x => x.Add(It.IsAny <Hospital>()), Times.Never);
            _dbContext.Verify(x => x.SaveChangesAsync(), Times.Never);
        }
        public async Task <CreateHospitalResponse> CreateAsync(CreateHospitalRequest request)
        {
            if (_dbContext.Hospitals.GetByName(request.Name) != null)
            {
                return(new CreateHospitalResponse {
                    Result = CreateHospitalResult.AlreadyExists
                });
            }

            //Todo: use AutoMapper
            //Note: relying on NServiceBus to handle exceptions and do exception logging
            var hospital = _dbContext.Hospitals.Add(new Hospital
            {
                Id        = Guid.NewGuid(),
                Name      = request.Name,
                Address   = request.Address,
                CreatedAt = DateTime.UtcNow
            });
            await _dbContext.SaveChangesAsync();

            return(new CreateHospitalResponse
            {
                Result = CreateHospitalResult.Success,
                Entity = hospital
            });
        }
Beispiel #5
0
        public async Task <CreateHospitalResponse> CreateHospital(CreateHospitalRequest request)
        {
            string resource    = $"/api/hospitals";
            var    restRequest = new RestRequest(resource, Method.POST);

            restRequest.AddParameter("application/json", JsonConvert.SerializeObject(request), ParameterType.RequestBody);
            IRestResponse response = await MakeCall(restRequest);

            return(JsonConvert.DeserializeObject <CreateHospitalResponse>(response.Content));
        }
        public async Task <CreateHospitalResponse> Create(CreateHospitalRequest request)
        {
            try
            {
                var hospital = new Hospital(request.Name, request.PublicKey);
                _dbContext.Hospitals.Add(hospital);
                await _dbContext.SaveChangesAsync();

                return(new CreateHospitalResponse(hospital));
            }
            catch (Exception ex)
            {
                // log details somewhere. i.e. inner exception, stacktrace, etc...
                return(new CreateHospitalResponse("There was an error creating a new Hospital"));
            }
        }
        public async Task Handle(CreateHospitalCommand message, IMessageHandlerContext context)
        {
            //TODO: Use Automapper
            var request = new CreateHospitalRequest
            {
                Name    = message.Name,
                Address = message.Address
            };

            //Create hospital
            var result = await _domainService.CreateAsync(request);

            Console.WriteLine("CreateHospitalCommand message received: " + JsonConvert.SerializeObject(message));

            // Publish HospitalCreatedEvent if operation successful
            if (result.Result == CreateHospitalResult.Success)
            {
                await context.Publish(
                    //Todo: use AutoMapper here
                    new HospitalCreatedEvent
                {
                    Id      = result.Entity.Id,
                    Name    = result.Entity.Name,
                    Address = result.Entity.Address
                });

                return;
            }

            // Publish HospitalCreateFailedEvent if hospital creation failed
            await context.Publish(
                //Todo: use AutoMapper here
                new HospitalCreateFailedEvent
            {
                Name    = message.Name,
                Address = message.Address
            });
        }
Beispiel #8
0
 public async Task <Hospital> CreateAsync(CreateHospitalRequest created)
 {
     return(await _mediator.RequestAsync <CreateHospitalRequest, Hospital>(created));
 }
Beispiel #9
0
        public async Task <ActionResult> Post([FromBody] CreateHospitalRequest request)
        {
            var response = await _hospitalService.Create(request);

            return(GetApiResponse(response));
        }