public async Task <OperationStatus> AddPresentationToUser(int presentationId, string userId) { var alreadyExistent = _context.UserPresentations.Any(p => p.PresentationId == presentationId && p.UserId == userId); if (!_context.Presentations.Any(p => p.Id == presentationId)) { return(new OperationStatus { ErrorMessageIfAny = "There is no presentation with the provided id" }); } if (alreadyExistent) { return(new OperationStatus { ErrorMessageIfAny = "The presentation already belongs to the user" }); } var up = new UserPresentation { UserId = userId, PresentationId = presentationId }; _context.UserPresentations.Add(up); var rows = await _context.SaveChangesAsync(); if (rows == 0) { return(new OperationStatus { ErrorMessageIfAny = "An error ocurred while updating the database" }); } return(new OperationStatus()); }
public async Task <IHttpActionResult> Post(CreateUser command) { await _domainLayer.CreateUserAsync(command); UserPresentation user = await _readModel.FindByIdAsync(command.UserId); return(CreatedAtRoute("DefaultApi", new { id = user.Id }, user)); }
private async Task <OperationStatus> ModifyPresentationByCopying(string userId, Presentation p, UploadPresentationModel updateModel) { var newPresentation = new Presentation { CategoryId = p.CategoryId, Name = p.Name, Description = p.Description, IsPublic = p.IsPublic, FileID = p.FileID, PresentationTags = new List <PresentationTag>() }; var list = await _context.UserPresentations.Where(u => u.UserId == userId && u.PresentationId == p.Id).ToListAsync(); _context.UserPresentations.Remove(list.First()); _context.Presentations.Add(newPresentation); var rows = await _context.SaveChangesAsync(); if (rows == 0) { return(new OperationStatus { ErrorMessageIfAny = "Error while trying to update the database" }); } var newUP = new UserPresentation { UserId = userId, PresentationId = newPresentation.Id }; _context.UserPresentations.Add(newUP); return(await ModifyPresentationInPlace(userId, newPresentation, updateModel)); }
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 }); }