public void PostAsync_With_Invalid_ModelState_Return_BadRequest()
        {
            //Arrange
            string expectedMessage    = "Invalid Model State";
            int    expectedStatusCode = 400;

            CreateTheatreModel createTheatreModel = new CreateTheatreModel
            {
                Name          = "bioskop",
                CityName      = "grad",
                NumberOfSeats = 13,
                SeatRows      = 13,
                AuditName     = "Sala1"
            };

            _theatreService = new Mock <ITheatreService>();
            _addressService = new Mock <IAddressService>();
            TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object);

            theatresController.ModelState.AddModelError("key", "Invalid Model State");


            //Act
            var result         = theatresController.PostAsync(createTheatreModel).ConfigureAwait(false).GetAwaiter().GetResult().Result;
            var resultResponse = (BadRequestObjectResult)result;
            var createdResult  = ((BadRequestObjectResult)result).Value;
            var errorResponse  = ((SerializableError)createdResult).GetValueOrDefault("key");
            var message        = (string[])errorResponse;

            //Assert
            Assert.IsNotNull(resultResponse);
            Assert.AreEqual(expectedMessage, message[0]);
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
            Assert.AreEqual(expectedStatusCode, resultResponse.StatusCode);
        }
        public void PostAsync_CreateTheatre_Throw_DbException_Theatre()
        {
            //Arrange
            string expectedMessage    = "Inner exception error message.";
            int    expectedStatusCode = 400;

            CreateTheatreModel createTheatreModel = new CreateTheatreModel
            {
                Name          = "Bioskop12345",
                CityName      = "grad",
                NumberOfSeats = 12,
                SeatRows      = 12,
                AuditName     = "Sala23"
            };

            TheatreDomainModel theatreDomainModel = new TheatreDomainModel
            {
                Id              = 123,
                AddressId       = 1423,
                Name            = createTheatreModel.Name,
                AuditoriumsList = new List <AuditoriumDomainModel>()
            };

            AddressDomainModel addressDomainModel = new AddressDomainModel
            {
                Id         = 123,
                CityName   = "grad123",
                StreetName = "ulica123"
            };

            Task <TheatreDomainModel> responseTask  = Task.FromResult(theatreDomainModel);
            Task <AddressDomainModel> responseTask2 = Task.FromResult(addressDomainModel);
            Exception         exception             = new Exception("Inner exception error message.");
            DbUpdateException dbUpdateException     = new DbUpdateException("Error.", exception);

            _theatreService = new Mock <ITheatreService>();
            _addressService = new Mock <IAddressService>();

            _theatreService.Setup(x => x.Create(It.IsAny <TheatreDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Throws(dbUpdateException);
            _addressService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2);
            TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object);

            //Act
            var result          = theatresController.PostAsync(createTheatreModel).ConfigureAwait(false).GetAwaiter().GetResult().Result;
            var badObjectResult = ((BadRequestObjectResult)result).Value;
            var errorResult     = (ErrorResponseModel)badObjectResult;

            //Assert
            Assert.IsNotNull(errorResult);
            Assert.AreEqual(expectedMessage, errorResult.ErrorMessage);
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
            Assert.AreEqual(expectedStatusCode, ((BadRequestObjectResult)result).StatusCode);
        }
        public void PostAsync_Create_IsSuccessful_True_Theatre()
        {
            //Arrange
            int expectedStatusCode = 201;

            CreateTheatreModel createTheatreModel = new CreateTheatreModel()
            {
                Name          = "bioskop123",
                CityName      = "grad",
                SeatRows      = 15,
                NumberOfSeats = 11,
                AuditName     = "Sala1"
            };

            AddressDomainModel addressDomainModel = new AddressDomainModel
            {
                Id         = 123,
                CityName   = "grad123",
                StreetName = "ulica123"
            };

            TheatreDomainModel theatreDomainModel = new TheatreDomainModel
            {
                AddressId       = 1234,
                Name            = createTheatreModel.Name,
                AuditoriumsList = new List <AuditoriumDomainModel>()
            };

            Task <TheatreDomainModel> responseTask  = Task.FromResult(theatreDomainModel);
            Task <AddressDomainModel> responseTask2 = Task.FromResult(addressDomainModel);

            _theatreService = new Mock <ITheatreService>();
            _addressService = new Mock <IAddressService>();
            _addressService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2);
            _theatreService.Setup(x => x.Create(It.IsAny <TheatreDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(responseTask);
            TheatresController theatreController = new TheatresController(_theatreService.Object, _addressService.Object);


            //Act
            var result               = theatreController.PostAsync(createTheatreModel).ConfigureAwait(false).GetAwaiter().GetResult().Result;
            var createdResult        = ((CreatedResult)result).Value;
            var theatreReturnedModel = (TheatreDomainModel)createdResult;

            //Assert
            Assert.IsNotNull(theatreReturnedModel);
            Assert.IsInstanceOfType(result, typeof(CreatedResult));
            Assert.AreEqual(expectedStatusCode, ((CreatedResult)result).StatusCode);
        }
        public async Task <ActionResult <TheatreDomainModel> > PostAsync([FromBody] CreateTheatreModel createTheatreModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            TheatreDomainModel theatreDomainModel = new TheatreDomainModel
            {
                Name       = createTheatreModel.Name,
                CityName   = createTheatreModel.CityName,
                StreetName = createTheatreModel.StreetName
            };

            TheatreDomainModel insertedModel;

            try
            {
                insertedModel = await _theatreService.Create(theatreDomainModel, createTheatreModel.NumberOfSeats, createTheatreModel.SeatRows, createTheatreModel.AuditName);
            }
            catch (DbUpdateException e)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel
                {
                    ErrorMessage = e.InnerException.Message ?? e.Message,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            if (insertedModel == null)
            {
                ErrorResponseModel errorResponse = new ErrorResponseModel
                {
                    ErrorMessage = Messages.THEATRE_CREATION_ERROR,
                    StatusCode   = System.Net.HttpStatusCode.BadRequest
                };

                return(BadRequest(errorResponse));
            }

            return(Created("theatres//" + insertedModel.Id, insertedModel));
        }
        public void PostAsync_Create_IsSuccessful_False_Return_BadRequest()
        {
            //Arrange
            string expectedMessage    = Messages.THEATRE_CREATION_ERROR;
            int    expectedStatusCode = 400;

            CreateTheatreModel createTheatreModel = new CreateTheatreModel
            {
                Name          = "bioskop",
                CityName      = "grad",
                NumberOfSeats = 13,
                SeatRows      = 13,
                AuditName     = "Sala12"
            };

            AddressDomainModel addressDomainModel = new AddressDomainModel
            {
                Id         = 123,
                CityName   = "grad123",
                StreetName = "ulica123"
            };

            TheatreDomainModel        theatreDomainModel = null;
            Task <TheatreDomainModel> responseTask       = Task.FromResult(theatreDomainModel);
            Task <AddressDomainModel> responseTask2      = Task.FromResult(addressDomainModel);

            _theatreService = new Mock <ITheatreService>();
            _addressService = new Mock <IAddressService>();
            _addressService.Setup(x => x.GetByCityNameAsync(It.IsAny <string>())).Returns(responseTask2);
            _theatreService.Setup(x => x.Create(It.IsAny <TheatreDomainModel>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns(responseTask);
            TheatresController theatresController = new TheatresController(_theatreService.Object, _addressService.Object);

            //Act
            var result          = theatresController.PostAsync(createTheatreModel).ConfigureAwait(false).GetAwaiter().GetResult().Result;
            var badObjectResult = ((BadRequestObjectResult)result).Value;
            var errorResult     = (ErrorResponseModel)badObjectResult;

            //Assert
            Assert.IsNotNull(errorResult);
            Assert.AreEqual(expectedMessage, errorResult.ErrorMessage);
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
            Assert.AreEqual(expectedStatusCode, ((BadRequestObjectResult)result).StatusCode);
        }