Ejemplo n.º 1
0
        public async Task <IActionResult> Post(RecordContract recordContract)
        {
            try
            {
                _logger.LogInformation("Posting a new record.");

                var record = _mapper.Map <Record>(recordContract);

                var result = await _repo.UpsertRecord(record);

                if (result.DateModified == null)
                {
                    var newUri = Url.Link("RecordGet", new { appName = record.ApplicationName, dataType = record.DataType, version = record.Version });
                    return(Created(newUri, _mapper.Map <RecordContract>(record)));
                }

                return(Ok(_mapper.Map <RecordContract>(result)));
            }
            catch (System.Exception e)
            {
                _logger.LogError(e.Message);
            }

            _logger.LogWarning($"Problem saving {recordContract.ApplicationName}/{recordContract.DataType}/{recordContract.Version}.");

            return(BadRequest("Could not update record"));
        }
Ejemplo n.º 2
0
        public void Post_Update()
        {
            // Arrange
            var recordContract = new RecordContract
            {
                ApplicationName = "testAppName",
                DataType        = "dataType",
                Version         = "version1",
                Value           = "value",
                DateCreated     = DateTime.UtcNow,
                DateModified    = DateTime.UtcNow
            };

            var req = new MockHttpRequest
            {
                Body = RecordFunctionTests.GenerateStreamFromString(_record)
            };
            var mapper = Substitute.For <IMapper>();
            var repo   = Substitute.For <IRepository>();

            repo.UpsertRecord(Arg.Any <Model.Record>()).Returns(Task.FromResult <Model.Record>(new Model.Record
            {
                DateModified = DateTime.Now
            }));

            var logger = Substitute.For <ILogger <Records> >();

            var target = new Records(repo, mapper);

            // Act
            var result = target.Post(req, logger).Result;

            // Assert
            Assert.IsType <OkObjectResult>(result);
        }
Ejemplo n.º 3
0
        public void Post_Throws()
        {
            // Arrange
            var recordContract = new RecordContract
            {
                ApplicationName = "testAppName",
                DataType        = "dataType",
                Version         = "version1",
                Value           = "value",
                DateCreated     = DateTime.UtcNow,
                DateModified    = DateTime.UtcNow
            };

            var mapper = Substitute.For <IMapper>();
            var repo   = Substitute.For <IRepository>();

            repo.UpsertRecord(Arg.Any <Model.Record>()).Returns(Task.FromException <Model.Record>(new System.Exception("Boom")));
            var logger = Substitute.For <ILogger <RecordsController> >();

            var target = new RecordsController(repo, logger, mapper);

            // Act
            var result = target.Post(recordContract).Result;

            // Assert
            logger.Received().Log(
                LogLevel.Error,
                Arg.Any <EventId>(),
                Arg.Is <object>(o => o.ToString() == "Boom"),
                null,
                Arg.Any <Func <object, Exception, string> >());

            Assert.IsType <BadRequestObjectResult>(result);
        }
Ejemplo n.º 4
0
        public void Post_Insert()
        {
            // Arrange
            var recordContract = new RecordContract
            {
                ApplicationName = "testAppName",
                DataType        = "dataType",
                Version         = "version1",
                Value           = "value"
            };

            var record = new Model.Record
            {
                ApplicationName = "testAppName",
                DataType        = "dataType",
                Version         = "version1",
                Value           = "value"
            };

            var mapper = Substitute.For <IMapper>();

            mapper.Map <Model.Record>(Arg.Any <RecordContract>()).Returns(record);

            var repo = Substitute.For <IRepository>();

            repo.UpsertRecord(Arg.Any <Model.Record>()).Returns(Task.FromResult <Model.Record>(record));

            var logger = Substitute.For <ILogger <RecordsController> >();

            var target = new RecordsController(repo, logger, mapper)
            {
                Url = Substitute.For <IUrlHelper>()
            };

            // Act
            var result = target.Post(recordContract).Result;

            // Assert
            Assert.IsType <CreatedResult>(result);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Post(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "Records")]
            HttpRequest req,
            ILogger log)
        {
            RecordContract recordContract = null;

            try
            {
                log.LogInformation("Posting a new record.");

                //read json object from request body
                var content = await new StreamReader(req.Body).ReadToEndAsync();
                recordContract = JsonConvert.DeserializeObject <Model.Contracts.RecordContract>(content);

                var record = _mapper.Map <Model.Record>(recordContract);

                var result = await _repo.UpsertRecord(record);

                if (result.DateModified == null)
                {
                    var newUri = $"Records/{record.ApplicationName}/{record.DataType}/{record.Version}";
                    return(new CreatedResult(newUri, _mapper.Map <RecordContract>(record)));
                }

                return(new OkObjectResult(_mapper.Map <RecordContract>(record)));
            }
            catch (Exception e)
            {
                log.LogError(e.Message);
            }

            log.LogWarning($"Problem saving {recordContract?.ApplicationName}/{recordContract?.DataType}/{recordContract?.Version}.");

            return(new BadRequestObjectResult("Could not update record"));
        }