public GenericResponse AddPost(AddPostRequest request) { GenericResponse response = new GenericResponse(); response.Errors = new List <string>(); bool validation = _authKeyRepository.Validate(request.RequesterId, request.AuthKey); if (!validation) { response.StatusCode = 400; response.Errors.Add("You do not have access"); } else { int postId = _postRepository.AddPost(request.RequesterId, request.CategoryId, request.Title, request.Content); foreach (var tag in request.Tags) { _tagRepository.AddToPost(postId, tag); } response.StatusCode = 200; string connString = "DefaultEndpointsProtocol=https;AccountName=timefunctionnip8ca1;AccountKey=yexSIebMMLZTojXbWS6QSvSOEoxXIqQW3l9oNjfpjdwVRrQKYgMQj1cgrrU7aHnVygp+TQPRsGoeq1UkWaxkUw==;EndpointSuffix=core.windows.net"; CloudStorageAccount.TryParse(connString, out var storageAccount); var cloudBlobClient = storageAccount.CreateCloudBlobClient(); var container = cloudBlobClient.GetContainerReference("files"); var cloudBlockBlob = container.GetBlockBlobReference(postId + ".png"); byte[] imageBytes = Convert.FromBase64String(request.Image); cloudBlockBlob.UploadFromByteArray(imageBytes, 0, imageBytes.Length); } return(response); }
public void AddTags(string tags, int postId) { if (tags.Contains(',')) { var words = tags.Split(", ", StringSplitOptions.RemoveEmptyEntries); foreach (var item in words) { var tagId = tagRepository.FindTagByContent(item); if (tagId == 0) { var newTag = new Tag { Content = item }; tagRepository.CreateTag(newTag); tagRepository.Save(); var newTagId = tagRepository.GetNewTagId(newTag); tagRepository.AddToPost(postId, newTagId); tagRepository.Save(); } if (postTagRepository.IsExistPostTag(postId, tagId)) { continue; } else { tagRepository.AddToPost(postId, tagId); tagRepository.Save(); } } } else { var tagId = tagRepository.FindTagByContent(tags); if (tagId == 0) { var newTag = new Tag { Content = tags }; tagRepository.CreateTag(newTag); tagRepository.Save(); var newTagId = tagRepository.GetNewTagId(newTag); tagRepository.AddToPost(postId, newTagId); tagRepository.Save(); } else { if (postTagRepository.IsExistPostTag(postId, tagId)) { return; } else { tagRepository.AddToPost(postId, tagId); tagRepository.Save(); } } } }