Skip to content

Commit

Permalink
zig/db- add Package.create
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Dec 13, 2021
1 parent 544d4a2 commit 6a0b46e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/db/Package.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const std = @import("std");
const string = []const u8;
const ulid = @import("ulid");
const extras = @import("extras");

const _db = @import("./_db.zig");
const Version = _db.Version;
const Time = _db.Time;
const User = _db.User;
const Remote = _db.Remote;

const _internal = @import("./_internal.zig");
const db = &_internal.db;

pub const Package = struct {
id: u64,
id: u64 = 0,
uuid: ulid.ULID,
owner: ulid.ULID,
name: string,
Expand All @@ -26,6 +29,26 @@ pub const Package = struct {

pub const table_name = "packages";

pub fn create(alloc: *std.mem.Allocator, owner: User, name: string, remote: Remote, rm_id: string, rm_name: string, desc: string, license: string, star_count: u64) !Package {
db.mutex.lock();
defer db.mutex.unlock();

return try _internal.insert(alloc, &Package{
.uuid = _internal.factory.newULID(),
.owner = owner.uuid,
.name = name,
.created_on = Time.now(),
.remote = remote.id,
.remote_id = rm_id,
.remote_name = rm_name,
.description = desc,
.license = license,
.latest_version = "",
.hook_secret = try extras.randomSlice(alloc, std.crypto.random, u8, 16),
.star_count = star_count,
});
}

usingnamespace _internal.ByKeyGen(Package);

pub fn latest(alloc: *std.mem.Allocator) ![]const Package {
Expand Down

0 comments on commit 6a0b46e

Please sign in to comment.