public async Task <ContentResponse> UploadContentAsync(IFormFile file, ContentType type) { try { if (file == null || file.Length == 0) { return(BaseResponse.Failure <ContentResponse>(HttpStatusCode.UnprocessableEntity, Constants.Validation.Content.InvalidFile(type))); } if (!Constants.Extensions.GetExtensions(type).Any(x => file.FileName.EndsWith(x))) { return(BaseResponse.Failure <ContentResponse>(HttpStatusCode.UnsupportedMediaType, Constants.Validation.Content.WrongFileExtension(type))); } var blobName = $@"{CutExtension(file.FileName)}-{Guid.NewGuid()}{ExtractExtension(file.FileName)}"; var url = await _blobStorageManager.UploadFileAsync(file, type, blobName); return(new ContentResponse { Url = url }); } catch (Exception ex) { return(BaseResponse.Failure <ContentResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public UserResponse GetUser(int id) { try { if (id <= 0) { return(BaseResponse.Failure <UserResponse>(HttpStatusCode.UnprocessableEntity, Constants.Validation.Users.IncorrectId())); } var user = _userRepository.GetUser(id); if (user == null) { return(BaseResponse.Failure <UserResponse>(HttpStatusCode.NotFound, Constants.Validation.Users.UserNotFound(id))); } return(new UserResponse { User = user }); } catch (SqlException ex) { return(BaseResponse.Failure <UserResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.SQLError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure <UserResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public BaseResponse DeleteUser(int id) { try { if (id <= 0) { return(BaseResponse.Failure(HttpStatusCode.BadRequest, Constants.Validation.Users.IncorrectId())); } if (_userRepository.DeleteUser(id) > 0) { return(BaseResponse.Success); } return(BaseResponse.Failure(HttpStatusCode.NotFound, Constants.Validation.Users.UserNotFound(id))); } catch (SqlException ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.SQLError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public UsersResponse GetUsers() { try { var response = _userRepository.GetUsers(); if (response == null || !response.Any()) { return(BaseResponse.Failure <UsersResponse>(HttpStatusCode.NotFound, Constants.Validation.Users.UsersNotFound())); } return(new UsersResponse { Users = response }); } catch (SqlException ex) { return(BaseResponse.Failure <UsersResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.SQLError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure <UsersResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public async Task <BaseResponse> DeleteBlogAsync(string id) { try { if (string.IsNullOrWhiteSpace(id)) { return(BaseResponse.Failure(HttpStatusCode.BadRequest, Constants.Validation.Blogs.IncorrectId())); } var deletedCount = await _blogRepository.DeleteBlogAsync(id); if (deletedCount > 0) { return(BaseResponse.Success); } return(BaseResponse.Failure(HttpStatusCode.NotFound, Constants.Validation.Blogs.BlogNotFound(id))); } catch (DocumentClientException ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.CosmosError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public BlogsResponse GetBlogs(IEnumerable <string> ids) { try { if (ids == null || !ids.Any()) { return(BaseResponse.Failure <BlogsResponse>(HttpStatusCode.BadRequest, Constants.Validation.Blogs.NoIdsRecieved())); } var response = _blogRepository.GetBlogs(ids); if (response == null || !response.Any()) { return(BaseResponse.Failure <BlogsResponse>(HttpStatusCode.NotFound, Constants.Validation.Blogs.BlogsNotFound())); } return(new BlogsResponse { Blogs = response }); } catch (DocumentClientException ex) { return(BaseResponse.Failure <BlogsResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.CosmosError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure <BlogsResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public BlogResponse GetBlog(string id) { try { if (string.IsNullOrWhiteSpace(id)) { return(BaseResponse.Failure <BlogResponse>(HttpStatusCode.BadRequest, Constants.Validation.Blogs.IncorrectId())); } var blog = _blogRepository.GetBlog(id); if (blog == null) { return(BaseResponse.Failure <BlogResponse>(HttpStatusCode.NotFound, Constants.Validation.Blogs.BlogNotFound(id))); } return(new BlogResponse { Blog = blog }); } catch (DocumentClientException ex) { return(BaseResponse.Failure <BlogResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.CosmosError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure <BlogResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public BaseResponse UpdateUser(int id, User user, ModelStateDictionary modelState) { try { if (user == null) { return(BaseResponse.Failure <UserCreateResponse>(HttpStatusCode.BadRequest, Constants.Validation.CommonErrors.IncorrectDataProvided())); } if (!modelState.IsValid) { var errors = modelState.Values .SelectMany(entry => entry.Errors) .Select(err => err.ErrorMessage) .Aggregate((f, s) => $"{f}; {s}"); return(BaseResponse.Failure(HttpStatusCode.BadRequest, errors)); } if (_userRepository.CheckIfSameEmailExists(user.Email) > 0) { return(BaseResponse.Failure(HttpStatusCode.Conflict, Constants.Validation.Users.SameUserExists())); } if (_userRepository.UpdateUser(user, id) > 0) { return(BaseResponse.Success); } return(BaseResponse.Failure(HttpStatusCode.NotFound, Constants.Validation.Users.UserNotFound(id))); } catch (SqlException ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.SQLError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public UserCreateResponse CreateUser(User user, ModelStateDictionary modelState) { try { if (user == null) { return(BaseResponse.Failure <UserCreateResponse>(HttpStatusCode.BadRequest, Constants.Validation.CommonErrors.IncorrectDataProvided())); } if (!modelState.IsValid) { var errors = modelState.Values .SelectMany(entry => entry.Errors) .Select(err => err.ErrorMessage) .Aggregate((f, s) => $"{f}; {s}"); return(BaseResponse.Failure <UserCreateResponse>(HttpStatusCode.BadRequest, errors)); } if (_userRepository.CheckIfSameEmailExists(user.Email) > 0) { return(BaseResponse.Failure <UserCreateResponse>(HttpStatusCode.Conflict, Constants.Validation.Users.SameUserExists())); } user.RegistrationDate = DateTime.Now; var id = _userRepository.CreateUser(user); return(new UserCreateResponse { Id = id }); } catch (SqlException ex) { return(BaseResponse.Failure <UserCreateResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.SQLError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure <UserCreateResponse>(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public async Task <BaseResponse> CreateBlogAsync(Blog blog, ModelStateDictionary modelState) { try { if (blog == null) { return(BaseResponse.Failure(HttpStatusCode.BadRequest, Constants.Validation.CommonErrors.IncorrectDataProvided())); } if (!modelState.IsValid) { var errors = modelState.Values .SelectMany(entry => entry.Errors) .Select(err => err.ErrorMessage) .Aggregate((f, s) => $"{f}; {s}"); return(BaseResponse.Failure(HttpStatusCode.BadRequest, errors)); } if (_blogRepository.CheckForExistedBlog(blog.Title)) { return(BaseResponse.Failure(HttpStatusCode.Conflict, Constants.Validation.Blogs.SameBlogTitleExists())); } blog.CreateDate = DateTime.Now; await _blogRepository.CreateBlogAsync(blog); return(BaseResponse.Success); } catch (DocumentClientException ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.CosmosError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }
public async Task <BaseResponse> RemoveContentAsync(string url, ContentType type) { try { if (string.IsNullOrWhiteSpace(url)) { return(BaseResponse.Failure(HttpStatusCode.BadRequest, Constants.Validation.Content.WrongUrl(url))); } var contentName = HttpUtility.UrlDecode(new Uri(url).Segments.Last()); var isDeleted = await _blobStorageManager.RemoveContentAsync(contentName, type); if (!isDeleted) { return(BaseResponse.Failure(HttpStatusCode.NotFound, Constants.Validation.Content.NotDeleted(url))); } return(BaseResponse.Success); } catch (FormatException) { return(BaseResponse.Failure(HttpStatusCode.UnprocessableEntity, Constants.Validation.Content.WrongUrl(url))); } catch (StorageException ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.BlobStorageError(ex.Message))); } catch (Exception ex) { return(BaseResponse.Failure(HttpStatusCode.InternalServerError, Constants.Validation.CommonErrors.ServerError(ex.Message))); } }