Skip to content

Modified Upload fike and Increment DownloadCount methods in file Serv… #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions Services/FilesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,31 @@ public async Task<string> DeleteFileAsync(string id)
});
}

public Task IncrementDownloadCountAsync(string id)
{
throw new NotImplementedException();
}
public async Task IncrementDownloadCountAsync(string id)
{
await Task.Run(() =>
{
var collection = _liteDatabase.GetCollection<FileDetails>("FileDetails");
var fileDetails = collection.FindById(id);
if (fileDetails != null)
{
// Increment the download count
fileDetails.DownloadCount++;
// Update the file details in the collection
var success = collection.Update(fileDetails);
if (!success)
{
throw new Exception("Error while updating download count");
}
}
else
{
throw new Exception("File not found");
}
});
}

public async Task<IEnumerable<FileDetails>> GetAllFileDetailsAsync()
public async Task<IEnumerable<FileDetails>> GetAllFileDetailsAsync()
{
return await Task.Run(() =>
{
Expand Down Expand Up @@ -92,16 +111,13 @@ public async Task<FileDetails> UpdateFileDetailsAsync(FileDetails details)
});
}

public async Task<FileDetails> UploadFileAsync(Stream fileStream, FileDetails fileDetails)
{
return await Task.Run(() =>
{
var collection = _liteDatabase.GetCollection<FileDetails>("FileDetails");
fileDetails.Id = ObjectId.NewObjectId().ToString();
collection.Insert(fileDetails.Id, fileDetails);
var obj = _liteDatabase.FileStorage.Upload(fileDetails.Id, fileDetails.Name, fileStream);
return fileDetails;
});
}
}
public async Task<FileDetails> UploadFileAsync(Stream fileStream, FileDetails fileDetails)
{
var collection = _liteDatabase.GetCollection<FileDetails>("FileDetails");
fileDetails.Id = ObjectId.NewObjectId().ToString();
collection.Insert(fileDetails.Id, fileDetails);
var obj = _liteDatabase.FileStorage.Upload(fileDetails.Id, fileDetails.Name, fileStream);
return fileDetails;
}
}
}