public async Task <OperationStatus> UploadPresentationForUser(string userId, UploadPresentationModel model) { var upList = await _context.UserPresentations.Where(userPresentation => userPresentation.UserId == userId) .Include(userp => userp.Presentation).Where(userp => userp.Presentation.Name == model.Name).ToListAsync(); if (upList.Count > 0) { return(new OperationStatus { ErrorMessageIfAny = OperationStatus.kPresentationWithSameNameExists }); } if (!_context.Categories.Any(c => c.Id == model.CategoryId)) { return(new OperationStatus { ErrorMessageIfAny = OperationStatus.kNoSuchCategoryWithId }); } var tagsForPresentationResult = await _tagsRepository.CreateOrGetTags(model.Tags); if (tagsForPresentationResult.ErrorMessageIfAny != null) { return(tagsForPresentationResult); } var tagsForPresentation = tagsForPresentationResult.Value; var currentPresentation = new Presentation { Name = model.Name, Description = model.Description, UserPresentations = new List <UserPresentation>(), CategoryId = model.CategoryId, PresentationTags = new List <PresentationTag>(), IsPublic = model.IsPublic }; var saveResult = await _filesRepository.SaveFile(model.SourceStream); if (saveResult.ErrorMessageIfAny != null) { return(saveResult); } currentPresentation.FileID = saveResult.Value; await _context.Presentations.AddAsync(currentPresentation); foreach (var tag in tagsForPresentation) { var pt = new PresentationTag { Tag = tag, Presentation = currentPresentation }; tag.PresentationTags.Add(pt); currentPresentation.PresentationTags.Add(pt); } var up = new UserPresentation { PresentationId = currentPresentation.Id, UserId = userId }; _context.UserPresentations.Add(up); int rows = await _context.SaveChangesAsync(); if (rows > 0) { return(new OperationStatus()); } return(new OperationStatus() { ErrorMessageIfAny = OperationStatus.kUnknownError }); }