Ejemplo n.º 1
0
        // Call an initialization - /system/init
        public IActionResult Init()
        {
            _sampleRepository.RemoveAllSamples();
            var name = _sampleRepository.CreateIndex();

            _sampleRepository.AddSample(new Sample()
            {
                FriendlyId = "1", Content = "Some sample content 1", CreateDate = DateTime.Now, ModifyDate = DateTime.Now, UserId = 1
            });
            _sampleRepository.AddSample(new Sample()
            {
                FriendlyId = "2", Content = "Some sample content 2", CreateDate = DateTime.Now, ModifyDate = DateTime.Now, UserId = 1
            });
            _sampleRepository.AddSample(new Sample()
            {
                FriendlyId = "3", Content = "Some sample content 3", CreateDate = DateTime.Now, ModifyDate = DateTime.Now, UserId = 2
            });
            _sampleRepository.AddSample(new Sample()
            {
                FriendlyId = "4", Content = "Some sample content 4", CreateDate = DateTime.Now, ModifyDate = DateTime.Now, UserId = 2
            });

            ViewBag.ResultMessage = "SamplesDb was created and filled with samples";

            return(View());
        }
        public async Task <ActionResult <SampleDto> > AddSample([FromBody] SampleForCreationDto sampleForCreation)
        {
            var validationResults = new SampleForCreationDtoValidator().Validate(sampleForCreation);

            validationResults.AddToModelState(ModelState, null);

            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
                //return ValidationProblem();
            }

            var sample = _mapper.Map <Sample>(sampleForCreation);
            await _sampleRepository.AddSample(sample);

            var saveSuccessful = await _sampleRepository.SaveAsync();

            if (saveSuccessful)
            {
                var sampleFromRepo = await _sampleRepository.GetSampleAsync(sample.SampleId);

                var sampleDto = _mapper.Map <SampleDto>(sampleFromRepo);
                var response  = new Response <SampleDto>(sampleDto);

                return(CreatedAtRoute("GetSample",
                                      new { sampleDto.SampleId },
                                      response));
            }

            return(StatusCode(500));
        }