Beispiel #1
0
        public async Task <Guid> AddLobbyAsync(Lobby lobby)
        {
            lobby.CreationDate = DateTime.Now;
            lobby.IsActive     = true;

            var currentUserId = await _userService.GetCurrentUserIdAsync();

            lobby.LobbyPlayers = new List <LobbyPlayer> {
                new() { PlayerId = currentUserId, IsCreator = true }
            };

            await _mnpContext.AddAsync(lobby);

            await _mnpContext.SaveChangesAsync();

            return(lobby.Id);
        }
Beispiel #2
0
        public async Task <File> UploadFileAsync(Stream stream, FileInfo fileInfo, bool needToSaveChanges = true)
        {
            EnsureArgumentsAreValid(stream, fileInfo);
            var fileHash = fileInfo.Hash;

            if (fileHash.IsNullOrWhiteSpace())
            {
                fileHash = ComputeMd5Hash(stream);
            }
            var fileInDb = await _mnpContext.Files.AsNoTracking().SingleOrDefaultAsync(f => f.Hash == fileHash);

            if (fileInDb != null)
            {
                _logger.LogInformation($"File with hash {fileHash} already uploaded.");
                return(fileInDb);
            }

            var mimeType   = fileInfo.MimeType ?? MimeTypes.GetMimeType(fileInfo.Extension);
            var filePath   = BuildFilePath(fileHash);
            var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            await stream.CopyToAsync(fileStream);

            await fileStream.DisposeAsync();

            var file = new File
            {
                CreationDate = DateTime.Now,
                FileLink     = "/Files/" + fileHash,
                Filename     = fileInfo.Filename ?? fileHash,
                Hash         = fileHash,
                MimeType     = mimeType
            };

            if (needToSaveChanges)
            {
                await _mnpContext.AddAsync(file);

                await _mnpContext.SaveChangesAsync();
            }

            return(file);
        }