public async Task <Guid> UploadFileAsync(Stream stream, string extension, FileType fileType) { File fileEntity = fileType switch { FileType.Caff => new CaffFile(), FileType.Preview => new PreviewFile(), _ => throw new ArgumentOutOfRangeException(nameof(fileType), fileType, null) }; fileEntity.Id = Guid.NewGuid(); fileEntity.Extension = extension; _context.Files.Add(fileEntity); var blobContainerClient = fileType switch { FileType.Caff => _caffBlobContainerClient, FileType.Preview => _previewBlobContainerClient, _ => throw new ArgumentOutOfRangeException(nameof(fileType), fileType, null) }; var blobName = fileEntity.Id + fileEntity.Extension; var blobClient = blobContainerClient.GetBlobClient(blobName); await blobClient.UploadAsync(stream); await _context.SaveChangesAsync(); return(fileEntity.Id); }
public async Task BuyAsync(long userId, long caffId) { var user = await _context.Users .Include(u => u.PurchasedCaffs) .FirstOrDefaultAsync(u => u.Id == userId); if (user == null) { throw new UserNotFoundException(); } if (user.PurchasedCaffs.Any(pc => pc.CaffId == caffId)) { throw new BadRequestException("Caff already purchased"); } var caff = await _context.Caffs.FirstOrDefaultAsync(c => c.Id == caffId); if (caff == null) { throw new BadRequestException("Not existing caff"); } user.PurchasedCaffs.Add(new PurchasedCaff { User = user, Caff = caff, PurchasedOn = DateTime.UtcNow }); await _context.SaveChangesAsync(); }
public async Task <CaffItemDetailsDto> UpdateCaffItemAsync(long caffItemId, UpdateCaffItemDto updateCaffItem) { var caffItemEntity = await _context .CaffItems .Include(ci => ci.CaffData) .ThenInclude(cd => cd.Animations) .ThenInclude(a => a.CiffData) .ThenInclude(cd => cd.Tags) .ThenInclude(t => t.Tag) .Include(ci => ci.PreviewFile) .Include(ci => ci.CreatedBy) .Where(ci => ci.Id == caffItemId) .SingleOrDefaultAsync(); ThrowNotFoundIfNull(caffItemEntity); _mapper.Map(updateCaffItem, caffItemEntity); await _context.SaveChangesAsync(); var response = _mapper.Map <CaffItemDetailsDto>(caffItemEntity); await _fileService.SetPreviewFileUri(response.PreviewFile); return(response); }
public async Task Add(long userId, long id, string text) { var comment = new Comment { AddTime = DateTime.Now, UserId = userId, CaffId = id, Text = text }; _context.Comments.Add(comment); await _context.SaveChangesAsync(); }
public async Task <CommentDto> UpdateMyCommentAsync(long commentId, UpdateCommentDto updateComment) { var commentEntity = await _context .Comments .Include(c => c.CreatedBy) .Where(c => c.Id == commentId) .SingleOrDefaultAsync(); ThrowNotFoundIfNull(commentEntity); ThrowForbiddenIfNotCreatedByCurrentUser(commentEntity); _mapper.Map(updateComment, commentEntity); await _context.SaveChangesAsync(); return(_mapper.Map <CommentDto>(commentEntity)); }
public async Task DeleteCommentAsync(long commentId) { var commentEntity = await _context .Comments .Where(c => c.Id == commentId) .SingleOrDefaultAsync(); // Deletion is idempotent if (commentEntity == null) { return; } _context.Comments.Remove(commentEntity); await _context.SaveChangesAsync(); }
public async Task DeleteMyUserProfileAsync() { var user = await _userManager.FindByIdAsync(_requestContext.CurrentUserId.ToString()); ThrowNotFoundIfNull(user); // Will result in soft delete var result = await _userManager.DeleteAsync(user); if (!result.Succeeded) { throw new CaffStoreBusinessException("Deleting user failed", result.Errors.Select(e => e.Description)); } // Delete soft deleted user data user.UserName = null; user.NormalizedUserName = null; user.Email = null; user.NormalizedEmail = null; user.FirstName = null; user.LastName = null; await _context.SaveChangesAsync(); }
public async Task <CaffItemDetailsDto> AddCaffItemAsync(AddCaffItemDto caffItem) { var parseResult = await CaffParser.ParseCaffFileAsync(caffItem.CaffFile.OpenReadStream()); if (!parseResult.Succeeded) { throw new CaffStoreBusinessException(parseResult.Message); } var caffData = _mapper.Map <CaffData>(parseResult.Result); // Get all tags var parsedTags = caffData.Animations.Select(a => a.CiffData.Tags).SelectMany(t => t).ToList(); var tagStrings = parsedTags.Select(t => t.Tag.Text).ToHashSet(); // Prefetch existing tags var existingTags = await _context .Tags .Where(t => tagStrings.Contains(t.Text)) .ToListAsync(); // If a tag is already in the database, do not store again, only set reference foreach (var parsedTag in parsedTags) { var existingTag = existingTags .FirstOrDefault(t => t.Text == parsedTag.Tag.Text); if (existingTag == null) { existingTags.Add(parsedTag.Tag); } else { parsedTag.Tag = existingTag; } } Guid caffFileId = await _fileService.UploadFileAsync(caffItem.CaffFile.OpenReadStream(), ".caff", FileType.Caff); Guid previewFileId; await using (var previewStream = new MemoryStream()) { parseResult.Preview.Save(previewStream, ImageFormat.Png); previewStream.Position = 0; previewFileId = await _fileService.UploadFileAsync(previewStream, ".png", FileType.Preview); } var caffItemEntity = new CaffItem { Title = caffItem.Title, Description = caffItem.Description, CaffFileId = caffFileId, PreviewFileId = previewFileId, CaffData = caffData, DownloadedTimes = 0, }; _context.CaffItems.Add(caffItemEntity); await _context.SaveChangesAsync(); return(await GetCaffItemAsync(caffItemEntity.Id)); }