public async Task<List<DomainProject>> GetListAsync(DomainProject entity) { List<ProjectEntity> projects = await _projectRepository.GetUserProjectsAsync(entity.UserId); if (!projects.Any()) { return new List<DomainProject>(); } return projects.Select(p => _mapper.Map<ProjectEntity, DomainProject>(p)).ToList(); }
public async Task<DomainProject> AddAsync(DomainProject entity) { entity.Created = DateTime.UtcNow; entity.Modified = DateTime.UtcNow; entity.HitsCount = 0; ProjectEntity projectEntity = _mapper.Map<DomainProject, ProjectEntity>(entity); ProjectEntity project = await _projectRepository.AddAsync(projectEntity); return _mapper.Map<ProjectEntity, DomainProject>(project); }
public async Task<DomainProject> GetAsync(DomainProject entity) { ProjectEntity project = await _projectRepository.GetAsync(entity.Id); if (project == null) { throw new NotFoundException(string.Format("Project with id '{0}' was not found.", entity.Id)); } if (project.UserId != entity.UserId) { throw new ForbiddenException(); } return _mapper.Map<ProjectEntity, DomainProject>(project); }
private async Task<ExampleProject> GetProjectAsync(DomainProject project) { if (project == null) { throw new ArgumentNullException("project"); } // Avsx Task<StorageFile> avsxTask = Task.FromResult<StorageFile>(null); if (!string.IsNullOrEmpty(project.AvsxFileId)) { avsxTask = _fileSystem.GetFilePropertiesSlimAsync(new StorageFile { Id = project.AvsxFileId }); } // Screenshot Task<StorageFile> screenshotTask = Task.FromResult<StorageFile>(null); if (!string.IsNullOrEmpty(project.ScreenshotFileId)) { screenshotTask = _fileSystem.GetFilePropertiesSlimAsync(new StorageFile { Id = project.ScreenshotFileId }); } // Original video Task<StorageFile> videoTask = Task.FromResult<StorageFile>(null); if (!string.IsNullOrEmpty(project.OriginalVideoFileId)) { videoTask = _fileSystem.GetFilePropertiesSlimAsync(new StorageFile { Id = project.OriginalVideoFileId }); } await Task.WhenAll(new[] { avsxTask, screenshotTask, videoTask }); var result = new ExampleProject { Id = project.Id, Name = project.Name, Description = project.Description, Created = project.Created, Views = project.HitsCount, Likes = project.LikesCount, Dislikes = project.DislikesCount, ProjectType = (int)project.ProjectType, ProjectSubtype = (int)project.ProjectSubtype }; if (avsxTask.Result != null) { result.AvsxUri = _uriProvider.CreateUri(avsxTask.Result.Id); result.TotalSize += avsxTask.Result.Length; } if (screenshotTask.Result != null) { result.ScreenshotUri = _uriProvider.CreateUri(screenshotTask.Result.Id); result.TotalSize += screenshotTask.Result.Length; } if (videoTask.Result != null) { result.VideoUri = _uriProvider.CreateUri(videoTask.Result.Id); result.VideoHash = videoTask.Result.Id; result.TotalSize += videoTask.Result.Length; } return result; }
public Task SendVideoCommentNotificationAsync(DomainUser user, DomainProject project, DomainComment domainComment) { var email = new SendEmailDomain { Address = _settings.EmailAddressInfo, DisplayName = Emails.SenderDisplayName, Emails = new List<string> { user.Email }, Subject = Emails.SubjectVideoComment, UserId = user.Id, Body = string.Format(PortalResources.VideoCommentNotification, user.Name, _userUriProvider.GetUri(domainComment.UserId), domainComment.UserName, _projectUriProvider.GetUri(project.Id), project.Name, domainComment.Body, _settings.PortalUri) }; // Send email on user registration return _emailSenderService.SendEmailAsync(email); }
public async Task<HttpResponseMessage> Post(ProjectModel model) { try { await CongratUserIfNeeded(); } catch (Exception e) { Trace.TraceError("Failed to notify user by email: {0}", e); } ProductType product = _productIdExtractor.Get(UserAgent); var project = new DomainProject { UserId = UserId, Name = model.Name, Description = model.Description, Access = model.Access, ProductType = product, ProjectType = model.ProjectType, ProjectSubtype = model.ProjectSubtype, EnableComments = model.EnableComments }; project = await _projectService.AddAsync(project); var responseProject = new Project { Id = project.Id, Description = project.Description, Access = project.Access, Name = project.Name, Created = project.Created, PublicUrl = _projectUriProvider.GetUri(project.Id), ProjectType = project.ProjectType, ProjectSubtype = project.ProjectSubtype, EnableComments = project.EnableComments }; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, responseProject); response.SetLastModifiedDate(project.Modified); return response; }
public async Task<HttpResponseMessage> Delete(string id) { var projectQuery = new DomainProject { UserId = UserId, Id = id }; await _projectService.DeleteAsync(projectQuery); return Request.CreateResponse(HttpStatusCode.OK); }
public async Task<HttpResponseMessage> Put(string id, ProjectModel model) { var project = new DomainProject { Id = id, UserId = UserId }; DomainProject originalProject = await _projectService.GetAsync(project); // updating original project originalProject.Name = model.Name; originalProject.Description = model.Description; originalProject.Access = model.Access; originalProject.EnableComments = model.EnableComments; project = await _projectService.EditAsync(originalProject); var responseProjects = new Project { Created = project.Created, Description = project.Description, Id = project.Id, Access = project.Access, Name = project.Name, PublicUrl = _projectUriProvider.GetUri(project.Id), ProjectType = project.ProjectType, ProjectSubtype = project.ProjectSubtype, EnableComments = project.EnableComments }; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, responseProjects); response.SetLastModifiedDate(project.Modified); return response; }
public async Task<HttpResponseMessage> Get(string id) { if (string.IsNullOrEmpty(id)) { return Request.CreateResponse(HttpStatusCode.BadRequest, ResponseMessages.ProjectInvalidId); } var project = new DomainProject { UserId = UserId, Id = id }; project = await _projectService.GetAsync(project); var responseProject = new Project { Id = project.Id, Description = project.Description, Name = project.Name, Access = project.Access, Created = project.Created, PublicUrl = _projectUriProvider.GetUri(project.Id), ProjectType = project.ProjectType, ProjectSubtype = project.ProjectSubtype, EnableComments = project.EnableComments }; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, responseProject); response.SetLastModifiedDate(project.Modified); return response; }
public async Task<HttpResponseMessage> Post(ExternalProjectModel model) { try { await CongratUserIfNeeded(); } catch (Exception e) { Trace.TraceError("Failed to notify user by email: {0}", e); } // Add project ProductType product = _productIdExtractor.Get(UserAgent); var project = new DomainProject { UserId = UserId, Name = model.Name, Description = model.Description, Access = model.Access, ProductType = product, ProjectType = model.ProjectType, ProjectSubtype = model.ProjectSubtype, EnableComments = model.EnableComments }; project = await _projectService.AddAsync(project); // Add external video await _externalVideoService.AddAsync(project.Id, new DomainExternalVideo { ProductName = model.ProductName, VideoUri = model.VideoUri }); // Add avsx file if exists if (model.Avsx != null) { var avsx = new DomainAvsx { ContentType = model.Avsx.ContentType, FileName = model.Avsx.Name, FileUri = model.Avsx.Uri, FileLength = model.Avsx.Length }; await _projectAvsxService.AddAsync(project.Id, avsx); // For statistics Request.Properties.Add("isAvsx", true); } // Add screenshot file if exists if (model.Screenshot != null) { var screenshot = new DomainScreenshot { FileName = model.Screenshot.Name, FileUri = model.Screenshot.Uri, FileId = Guid.NewGuid().ToString(), FileLength = model.Screenshot.Length, ContentType = model.Screenshot.ContentType, Created = DateTime.UtcNow, Modified = DateTime.UtcNow }; await _projectScreenshotService.AddAsync(project.Id, screenshot); // For statistics Request.Properties.Add("isScreenshot", true); } var responseProject = new Project { Id = project.Id, Description = project.Description, Access = project.Access, Name = project.Name, Created = project.Created, PublicUrl = _projectUriProvider.GetUri(project.Id), ProjectType = project.ProjectType, ProjectSubtype = project.ProjectSubtype, EnableComments = project.EnableComments }; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, responseProject); response.SetLastModifiedDate(project.Modified); return response; }
public async Task<DomainProject> EditAsync(DomainProject entity) { ProjectEntity project = await _projectRepository.GetAsync(entity.Id); if (project.UserId != entity.UserId) { throw new ForbiddenException(); } ProjectEntity updatedProject = _mapper.Map<DomainProject, ProjectEntity>(entity); // Overwriting not-modified fields updatedProject.Created = project.Created; updatedProject.Modified = DateTime.UtcNow; updatedProject.ProjectType = project.ProjectType; updatedProject.ProjectSubtype = project.ProjectSubtype; updatedProject.ProductId = project.ProductId; updatedProject.HitsCount = project.HitsCount; updatedProject = await _projectRepository.UpdateAsync(updatedProject); return _mapper.Map<ProjectEntity, DomainProject>(updatedProject); }
public async Task DeleteAsync(DomainProject entity) { ProjectEntity project = await _projectRepository.GetAsync(entity.Id); if (project == null) { throw new NotFoundException(); } if (project.UserId != entity.UserId) { throw new ForbiddenException(); } var tasks = new List<Task>(); // removing avsx if (!string.IsNullOrEmpty(project.AvsxFileId)) { tasks.Add(_fileSystem.DeleteFileAsync(new StorageFile { Id = project.AvsxFileId, UserId = project.UserId })); } // removing video if (!string.IsNullOrEmpty(project.OriginalVideoFileId)) { tasks.Add(_fileSystem.DeleteFileAsync(new StorageFile { Id = project.OriginalVideoFileId, UserId = project.UserId })); } // removing screenshot if (!string.IsNullOrEmpty(project.ScreenshotFileId)) { tasks.Add(_fileSystem.DeleteFileAsync(new StorageFile { Id = project.ScreenshotFileId, UserId = project.UserId })); } // removing encoded screenshots tasks.AddRange(project.EncodedScreenshots.Select(s => _fileSystem.DeleteFileAsync(new StorageFile { Id = s.FileId, UserId = project.UserId }))); // removing encoded videos tasks.AddRange(project.EncodedVideos.Select(s => _fileSystem.DeleteFileAsync(new StorageFile { Id = s.FileId, UserId = project.UserId }))); try { await Task.WhenAll(tasks); } catch (Exception e) { // could not delete all related files Trace.TraceError("Could not delete all related files for project {0}: {1}", project.Id, e); } // removing project with all embedded data: avsx, video, screenshot await _projectRepository.DeleteAsync(project.Id); }