public JsonResult Create(NewAuthorViewModel model)
        {
            // TODO: Add insert logic here
            if (ModelState.IsValid)
            {
                IdType idType = null;
                try
                {
                    idType = dbContext.idTypes.Single(m => m.id == model.idTypeId);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Excepcion", ex.Message);
                }

                if (idType == null)
                {
                    ModelState.AddModelError("idTypeId", "Tipo de identificación Inválido");
                }
                else if (dbContext.authors.All(m => !(m.authorId == model.authorId && m.idType.id == model.idTypeId)))
                {
                    dbContext.authors.Add(new Author {
                        idType = idType, authorId = model.authorId, name = model.name
                    });
                    dbContext.SaveChanges();

                    return(Json(new { status = "OK",
                                      info = "Operación exitosa!" }));
                }
                else
                {
                    ModelState.AddModelError("authorId", "El autor ya está registrado!");
                }
            }

            List <string> messages = new List <string>();

            foreach (var ms in ModelState.Values)
            {
                foreach (var me in ms.Errors)
                {
                    messages.Add(me.ErrorMessage);
                }
            }
            return(Json(new { status = "ERROR", info = messages }));
        }
        public async Task <ActionResult> Create(NewAuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                db.Authours.Add(new Author()
                {
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    Email       = model.Email,
                    Address     = model.Address,
                    PhoneNumber = model.PhoneNumber,
                    CreateDate  = DateTime.Now,
                    LastUpdate  = DateTime.Now
                });
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Beispiel #3
0
        public async Task <IActionResult> UpdateAuthor([FromBody] NewAuthorViewModel author)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var newAuthor = Mapper.Map <Author>(author);
                    await _bookService.UpdateAuthorAsync(newAuthor);

                    return(Ok("Author successfully updated"));
                }
                catch (Exception e)
                {
                    return(StatusCode(500, "Server error. Try to reload page."));
                }
            }
            else
            {
                return(StatusCode(400, "Invalid data format."));
            }
        }