public async Task <GenericResult> Save(T category) { var result = new GenericResult(); if (IsValidName(category.Name)) { category.SerializedName = category.Name.ToUpper().Replace(" ", ""); if (await IsRepeated(category.SerializedName) && category.Id == 0) { result.AddError(RepeatedCategoryError); } if (result.Success) { if (category.Id > 0) { if (!await Update(category)) { result.AddError(SavingError); } } else if (!await Insert(category)) { result.AddError(SavingError); } } } else { result.AddError(InvalidNameError); } return(result); }
public async Task <GenericResult> Save(AnimalType animalType) { GenericResult result = new GenericResult(); if (IsValidName(animalType.Name)) { animalType.SerializedName = animalType.Name.ToUpper().Replace(" ", ""); if (await IsRepeated(animalType.SerializedName) && animalType.Id == 0)// Si estamos repitiendo y no estamos editando { //Devolver result con mensaje de error result.AddError(REPEATED_ANIMAL_TYPE_ERROR); } if (result.Success) { if (animalType.Id > 0) { if (!await Update(animalType)) { result.AddError(SAVING_ERROR); } } else if (!await Insert(animalType)) { result.AddError(SAVING_ERROR); } } } else { result.AddError(INVALID_NAME_ERROR); } return(result); }
public async Task <GenericResult> Save(City city) { GenericResult result = new GenericResult(); if (IsValidName(city.Name)) { city.SerializedName = city.Name.ToUpper().Replace(" ", ""); if (await IsRepeated(city.SerializedName) && city.Id == 0) { //Devolver result con mensaje de error result.AddError(REPEATED_CITY_ERROR); } if (result.Success) { if (city.Id > 0) { if (!await Update(city)) { result.AddError(SAVING_ERROR); } } else if (!await Insert(city)) { result.AddError(SAVING_ERROR); } } } else { result.AddError(INVALID_NAME_ERROR); } return(result); }
public async Task <GenericResult <string> > UploadPetPhotoAsync(IFileListEntry fileEntry) { GenericResult <string> result = new GenericResult <string>(); CreateImagesFolder(); // Creo el directorio donde voy a guardar las imagenes if (fileEntry == null) { result.AddError(EMPTY_FILE_ERROR); return(result); } string fileType = fileEntry.Type; if (ImageTypes.Contains(fileType)) { string fileExtension = fileType.Replace("image/", string.Empty); var uniqueFileName = string.Format(@"{0}.{1}", Guid.NewGuid(), fileExtension); var path = Path.Combine(_environment.ContentRootPath, "wwwroot/images", uniqueFileName); var ms = new MemoryStream(); await fileEntry.Data.CopyToAsync(ms); using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write)) { ms.WriteTo(file); } result.value = uniqueFileName; } else { result.AddError(INVALID_FILE_TYPE); } return(result); }
public async Task <GenericResult> HasCorrectData(Comment comment) { GenericResult result = new GenericResult(); if (await _petService.Get(comment.PetId) == null) { result.AddError(ERROR_WRONG_PET); } if (await _userManager.FindByIdAsync(comment.UserId) == null) { result.AddError(ERROR_WRONG_USER); } return(result); }
public async Task <GenericResult> HasCorrectData(Comment comment) { var result = new GenericResult(); if (await _petService.Get(comment.PetId) == null) { result.AddError(ErrorWrongPet); } if (await _userManager.FindByIdAsync(comment.UserId) == null) { result.AddError(ErrorWrongUser); } return(result); }
public async Task <GenericResult> Save(Pet pet, IFileListEntry photo) { var result = new GenericResult(); var appUser = await _applicationUserService.GetCurrent(); if (appUser == null) { result.AddError(ErrorInvalidUser); return(result); } pet.UserId = appUser.Id; if (photo != null) // Puede ser que la imagen sea nula porque estemos editando y no cambiamos la foto { var resultImage = await _fileService.UploadPetPhotoAsync(photo); if (resultImage.Success) { pet.Photo = resultImage.Value; // Si la imagen se subio bien le asignamos la url a la mascota } else { result.AddRange(resultImage.Errors); } } result.Errors.AddRange(CheckPet(pet)); // Si devuelve errores los agrego para mostrarlos if (result.Success) // Si no hay errores guardo o actualizo { if (pet.Id > 0) // Si estamos editando { result.AddRange((await Update(pet)).Errors); } else if (!await Insert(pet)) // Si estamos guardando { result.AddError(ErrorSaving); } } return(result); }
public async Task <GenericResult> Update(Pet pet) { GenericResult result = new GenericResult(); if (await CurrUserCanEdit(pet)) { _context.Entry(pet).State = EntityState.Modified; if (await _context.SaveChangesAsync() > 0) { return(result); } else { result.AddError(ERROR_SAVING); } } else { result.AddError(ERROR_INVALID_USER); } return(result); }
public async Task <GenericResult> Update(Comment comment) { GenericResult hasCorrectData = await HasCorrectData(comment); if (hasCorrectData.Success) { bool commentExists = await Exists(comment.Id); if (commentExists) { _context.Entry(comment).State = EntityState.Modified; if (await _context.SaveChangesAsync() == 0) { hasCorrectData.AddError(ERROR_SAVING_COMMENT); } } else { hasCorrectData.AddError(ERROR_COMMENT_NOT_FOUND); } } return(hasCorrectData); }
public async Task <GenericResult> Insert(Comment comment) { GenericResult hasCorrectData = await HasCorrectData(comment); if (hasCorrectData.Success) { _context.Comments.Add(comment); if (await _context.SaveChangesAsync() == 0) { hasCorrectData.AddError(ERROR_SAVING_COMMENT); } } return(hasCorrectData); }
public async Task <GenericResult <string> > GetJwt(string email, string password) { var user = new LoginModel { Email = email, Password = password }; var jsonUser = JsonConvert.SerializeObject(user); // Preparo content del post var bufferUser = Encoding.UTF8.GetBytes(jsonUser); var byteContent = new ByteArrayContent(bufferUser); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var result = new GenericResult <string>(); try { // POST api/auth var response = await _httpClient.PostAsync(_urlApiAuth, byteContent); if (response.IsSuccessStatusCode) // Deberia devolver algo así {token: [jwt]} { var source = await response.Content.ReadAsStringAsync(); var tokenObj = JObject.Parse(source); result.Value = Convert.ToString(tokenObj["token"]); } else { result.AddError(response.StatusCode.ToString()); } } catch (HttpRequestException exception) { result.AddError("Error al establecer la conexión. Código de error: " + exception); } return(result); }
public async Task <GenericResult> Update(Pet pet) { var result = new GenericResult(); if (await CurrentUserCanEdit(pet)) { _context.Entry(pet).State = EntityState.Modified; if (pet.Photo == null) { _context.Entry(pet).Property(x => x.Photo).IsModified = false; } if (await _context.SaveChangesAsync() > 0) { return(result); } result.AddError(ErrorSaving); } else { result.AddError(ErrorInvalidUser); } return(result); }