public bool CreatePet(CreatePetViewModel model) { try { var pet = new Data.Models.Pet() { ID = Guid.NewGuid(), Specie = (Enums.Pet.Specie)model.Specie, Name = model.Name, Breed = model.Breed, Size = model.Specie == 0 ? (Enums.Pet.Size)(model.Size ?? 0) : Enums.Pet.Size.Default, Alergies = model.Alergies, Description = model.Description, AllowsPhotos = model.AllowsPhotos, Status = model.Status, OwnerID = model.OwnerID }; _unitOfWork.Pet.Add(pet); _unitOfWork.Commit(); return(true); } catch (Exception ex) { return(false); } }
public ActionResult Create() { var model = new CreatePetViewModel(); model.DropDownBreed = db.Pets.Select(p => p.Breed).Distinct().Select(p => new SelectListItem { Value = p, Text = p }).ToList(); return(View(model)); }
public async Task <ActionResult> Create([FromRoute] long id, [FromBody] CreatePetViewModel request) { var domainPet = _mapper.Map <Pet>(request); domainPet.OwnerId = id; // TODO: Add validation here. var createdPet = await _petRepository.AddAsync(domainPet); return(Ok(_mapper.Map <PetViewModel>(createdPet))); }
public ActionResult Create(CreatePetViewModel model) { if (!ModelState.IsValid) { GetViewBags(); ViewBag.Clients = _petService.GetActiveClient(); return(View(model)); } var result = _petService.CreatePet(model); if (result) { SetSessionNotification("Pet criado com sucesso!", "alert-success"); return(RedirectToAction("Index")); } else { SetSessionNotification("Não foi possível criar o pet.", "alert-danger"); return(RedirectToAction("Index")); } }
public async Task <IActionResult> Pets(long userId, CreatePetViewModel model) { try { if (_db.User.Find(userId) == null) { return(NotFound(ErrorMessage.UserNotFound())); } if (_db.Breed.Find(model.BreedId) == null) { return(NotFound(ErrorMessage.BreedNotFound())); } DateTime birthDatetime; if (!DateTime.TryParse(model.BirthDatetime, out birthDatetime)) { return(BadRequest(ErrorMessage.DateTimeWrongFormat())); } var pet = _mapper.Map <Pet>(model); pet.birth_datetime = birthDatetime; pet.user_id = userId; if (!_db.Pet.Any(x => x.user_id == userId)) { pet.is_current = true; } _db.Pet.Add(pet); await _db.SaveChangesAsync(); await Images(pet.pet_id, model.PetImages); return(Json(pet)); } catch (TimeoutException e) { return(BadRequest(ErrorMessage.Exception(e.Message))); } }