<?php

namespace Subroutes;

trait Files
{
	protected function answer_Files($resourceName, $id, $field) {
		$class = "\\Resources\\".$resourceName;
		$resource = $class::Get()->Ref($id);
		switch (\Request::Method()) {
			case "POST":
				if ($this->can("upload", $id, ["field" => $field]) && $resource->FileUpload($field)) {
					\Response::Get()->FileUploaded();
					return true;
				}
				break;
			case "DELETE":
				if ($this->can("erase", $id, ["field" => $field]) && $resource->FileErase($field)) {
					\Response::Get()->FileErased();
					return true;
				}
				break;
			default:
    		\Response::Get()->NotImplemented();
		}
		return false;
	}
}
