You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.0 KiB

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