Ejemplo n.º 1
0
        public async Task <ActionResult> Create([FromBody] AuthorCreateModel model)
        {
            if (Request.Cookies.ContainsKey("role"))
            {
                var role = Request.Cookies["admin"];
                if (role != "admin")
                {
                    return(BadRequest("Error"));
                }
            }
            else
            {
                return(BadRequest("Error"));
            }

            try
            {
                using var service = _authorService;
                await service.Create(model);

                return(Ok());
            }
            catch (Exception)
            {
                return(BadRequest("Error"));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateAuthor(AuthorCreateModel model)
        {
            var command  = new CreateAuthorCommand(model.FirstName, model.LastName, model.BirthDate);
            var response = await _mediator.Send(command);

            return(response.ToActionResult());
        }
Ejemplo n.º 3
0
        public async Task Create(AuthorCreateModel authorCreateModel)
        {
            if (authorCreateModel == null ||
                authorCreateModel.FirstName.IsNullOrEmpty() ||
                authorCreateModel.SecondName.IsNullOrEmpty())
            {
                throw new CustomException("Некоректные данные");
            }
            var authors = await _authorRepository.GetAll();

            if (authors == null)
            {
                throw new ServerException("Сервер вернул null");
            }
            if (authors.Any(w =>
                            string.Equals(w.FirstName, authorCreateModel.FirstName) &&
                            string.Equals(w.SecondName, authorCreateModel.SecondName)))
            {
                throw new CustomException("Такой автор уже существует");
            }
            var result = await _authorRepository.Add(_mapper.Map <Author>(authorCreateModel));

            if (result == null)
            {
                throw new ServerException("Ошибка в БД");
            }
        }
Ejemplo n.º 4
0
 public ActionResult Create(AuthorCreateModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             int id;
             if (model.Photo != null)
             {
                 string filepath = Server.MapPath("~/App_Data/Uploads/Covers/Authors/" +
                                                  FilePathGenerator.GenerateFileName(model.Photo.FileName));
                 model.Photo.SaveAs(filepath);
                 id = service.AddAuthor(model.ToServiceAuthor(filepath));
             }
             else
             {
                 id = service.AddAuthor(model.ToServiceAuthor());
             }
             return(RedirectToAction("Details", new { id = id }));
         }
         return(View(model));
     }
     catch (Exception ex)
     {
         logger.Error(ex);
         return(View("Error"));
     }
 }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create([FromBody] AuthorCreateModel author)
        {
            if (ModelState.IsValid)
            {
                AuthorResultModel response = await this.authorService.CreateAuthor(author.Name, author.ImageUrl, author.Website, author.Description, author.Books);

                if (!response.Success)
                {
                    FailedResponseModel badResponse = new FailedResponseModel()
                    {
                        Errors = response.Errors
                    };

                    return(BadRequest(badResponse));
                }

                AuthorSuccessResponseModel successResponse = new AuthorSuccessResponseModel()
                {
                    Name = response.Name
                };

                return(Ok(successResponse));
            }

            return(BadRequest(new FailedResponseModel {
                Errors = ModelState.Values.SelectMany(x => x.Errors.Select(y => y.ErrorMessage))
            }));
        }
Ejemplo n.º 6
0
        public int Post(AuthorCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                throw new Exception("Form is invalid.");
            }

            return(_authorService.AddAuthor(model, _userManager.GetUserId(User)));
        }
 public int AddAuthor(AuthorCreateModel model)
 {
     return(_authorRepository.Create(new DbAuthor
     {
         BookId = model.BookId,
         FirstName = model.AuthorFirstName,
         LastName = model.AuthorLastName
     }));
 }
        public IHttpActionResult Post(AuthorCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var id = _bookStoreManager.AddAuthor(model);

            return(Created(Url.Content($"/edit/{model.BookId}"), id));
        }
Ejemplo n.º 9
0
 public static ServiceAuthor ToServiceAuthor(this AuthorCreateModel author)
 {
     return(new ServiceAuthor()
     {
         Name = author.Name,
         BirthDate = author.BirthDate,
         DeathDate = author.DeathDate,
         Biography = author.Biography
     });
 }
Ejemplo n.º 10
0
 public static ServiceAuthor ToServiceAuthor(this AuthorCreateModel author, string filepath)
 {
     return(new ServiceAuthor()
     {
         Name = author.Name,
         BirthDate = author.BirthDate,
         DeathDate = author.DeathDate,
         Biography = author.Biography,
         Photo = filepath
     });
 }
Ejemplo n.º 11
0
        public async Task <IActionResult> Create(AuthorCreateModel collection)
        {
            if (ModelState.IsValid)
            {
                await m_authorService.Create(collection);

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(collection));
            }
        }
Ejemplo n.º 12
0
        public void Create_BadArgument_Exception()
        {
            var authorCreateModel = new AuthorCreateModel
            {
                FirstName  = "SomeAuthorFirstName",
                SecondName = "SomeAuthorSecondName",
                ImageUrl   = "ImageUrl",
                BooksId    = new List <string>()
            };

            _mockAuthorRepository.Setup(w => w.GetAll()).ReturnsAsync(_authors);
            using var authorService = new AuthorService(_mockAuthorRepository.Object, _mapper);
            Assert.ThrowsAsync <CustomException>(() => authorService.Create(authorCreateModel));
        }
Ejemplo n.º 13
0
        public void Create_GoodArgument_ReturnNull2_Exception()
        {
            var authorCreateModel = new AuthorCreateModel
            {
                FirstName  = "FirstName",
                SecondName = "SecondName",
                ImageUrl   = "ImageUrl",
                BooksId    = new List <string>()
            };

            _mockAuthorRepository.Setup(w => w.GetAll()).ReturnsAsync(_authors);
            _mockAuthorRepository.Setup(w => w.Add(It.IsAny <Author>())).ReturnsAsync(() => null);
            using var authorService = new AuthorService(_mockAuthorRepository.Object, _mapper);
            Assert.ThrowsAsync <ServerException>(() => authorService.Create(authorCreateModel));
        }
Ejemplo n.º 14
0
        public IActionResult Create([Bind("Name, Description")] AuthorCreateModel authorCreateModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(authorCreateModel));
            }

            _repository.CreateAuthor(
                Author.CreateAuthor(
                    authorCreateModel.Name,
                    authorCreateModel.Description
                    )
                );

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 15
0
        public IHttpActionResult Post([FromBody] AuthorCreateModel author)
        {
            IHttpActionResult badRequest;

            if (!this.IsModelValid(ModelState, author, out badRequest))
            {
                return(badRequest);
            }

            var authorEntity = Mapper.Map <Author>(author);

            Database <Author> .Create(authorEntity);

            var authorReadModel = Mapper.Map <AuthorReadModel>(authorEntity);

            return(CreatedAtRoute("DefaultApi", new { controller = "author", id = authorReadModel.Id }, authorReadModel));
        }
Ejemplo n.º 16
0
        public void Create_GoodArgument_Success()
        {
            var authorCreateModel = new AuthorCreateModel
            {
                FirstName  = "FirstName",
                SecondName = "SecondName",
                ImageUrl   = "ImageUrl",
                BooksId    = new List <string>()
            };

            _mockAuthorRepository.Setup(w => w.GetAll()).ReturnsAsync(_authors);
            _mockAuthorRepository.Setup(w => w.Add(It.IsAny <Author>())).ReturnsAsync(_author);
            using var authorService = new AuthorService(_mockAuthorRepository.Object, _mapper);
            var create = authorService.Create(authorCreateModel);

            _mockAuthorRepository.Verify(w => w.Add(It.IsAny <Author>()), Times.Once);
        }
Ejemplo n.º 17
0
        public async Task <HttpResponseMessage> Add([FromBody] AuthorCreateModel model)
        {
            HttpResponseMessage returnMessage = new HttpResponseMessage();

            var role   = mapper.Map <AuthorDto>(model);
            var result = await service.CreateAsync(role);

            if (result.IsSuccess)
            {
                string message = ($"Student Created - {result.Entity.Id}");
                returnMessage = new HttpResponseMessage(HttpStatusCode.Created);
                returnMessage.RequestMessage = new HttpRequestMessage(HttpMethod.Post, message);
            }
            else
            {
                returnMessage = new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
                returnMessage.RequestMessage = new HttpRequestMessage(HttpMethod.Post, result.GetErrorString());
            }

            return(returnMessage);
        }
Ejemplo n.º 18
0
        public IHttpActionResult Post([FromBody] AuthorCreateModel authorModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var newAuthor = Mapper.Map <Author>(authorModel);

            // add existing books
            foreach (var bookId in authorModel.Books)
            {
                var existingBook = _bookRepository.Find(bookId);
                if (existingBook != null)
                {
                    newAuthor.Books.Add(existingBook);
                }
            }
            var id       = _authorRepository.Add(newAuthor);
            var location = new Uri(Request.RequestUri + "/" + id);

            return(Created(location, id));
        }
Ejemplo n.º 19
0
 public async Task Create(AuthorCreateModel model)
 {
     await m_repository.Add(m_automapper.Map <Author>(model));
 }