54 lines
1.0 KiB
Crystal
54 lines
1.0 KiB
Crystal
|
require "uuid"
|
||
|
require "file_utils"
|
||
|
|
||
|
require "./task.cr"
|
||
|
require "./column.cr"
|
||
|
|
||
|
class Project
|
||
|
JSON.mapping({
|
||
|
id: String,
|
||
|
name: String,
|
||
|
columns: Array(Column),
|
||
|
tasks: Array(Task),
|
||
|
color: {
|
||
|
type: String,
|
||
|
default: "dark"
|
||
|
}
|
||
|
})
|
||
|
|
||
|
def initialize(@name)
|
||
|
@id = UUID.random.to_s
|
||
|
|
||
|
@columns = [Column.new "To do"]
|
||
|
@tasks = [] of Task
|
||
|
|
||
|
@color = "dark"
|
||
|
end
|
||
|
|
||
|
def write!(storage)
|
||
|
Dir.mkdir_p "#{storage}/#{@id}"
|
||
|
|
||
|
File.write "#{storage}/#{@id}/project.json.new", to_json
|
||
|
FileUtils.mv "#{storage}/#{@id}/project.json.new",
|
||
|
"#{storage}/#{@id}/project.json"
|
||
|
end
|
||
|
|
||
|
# FIXME: Transform that exception somehow.
|
||
|
# FIXME: Update to not read everything.
|
||
|
def self.get_from_id(id, storage)
|
||
|
self.all(storage).find(&.id.==(id)).not_nil!
|
||
|
end
|
||
|
|
||
|
def self.all(storage) : Array(Project)
|
||
|
# FIXME: switch to an index file per project in a storage directory.
|
||
|
Dir.children(storage).compact_map do |filename|
|
||
|
begin
|
||
|
Project.from_json File.read "#{storage}/#{filename}/project.json"
|
||
|
rescue
|
||
|
end
|
||
|
end
|
||
|
rescue
|
||
|
[] of Project
|
||
|
end
|
||
|
end
|