Skip to content
Merged
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions App/AL/Controller/Project/Image/ProjectImageController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using App.DL.Repository.Project;
using App.PL.Transformer.Image;
using Micron.AL.Validation.Db;
using Micron.DL.Middleware;
using Micron.DL.Module.Controller;
using Micron.DL.Module.Http;
using Micron.DL.Module.Validator;

namespace App.AL.Controller.Project.Image {
public class ProjectImageController : BaseController {
protected override IMiddleware[] Middleware() => new IMiddleware[] { };

public ProjectImageController() {
Get("/api/v1/project/images/get", _ => {
var errors = ValidationProcessor.Process(Request, new IValidatorRule[] {
new ExistsInTable("project_guid", "projects", "guid"),
});
if (errors.Count > 0) return HttpResponse.Errors(errors);

var project = ProjectRepository.FindByGuid(GetRequestStr("project_guid"));

return HttpResponse.Item("images", new ImageTransformer().Many(project.Images()));
});
}
}
}
36 changes: 36 additions & 0 deletions App/DL/Model/Image/Image.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Linq;
using Dapper;

namespace App.DL.Model.Image {
public class Image : Micron.DL.Model.Model {
public int id;

public string guid;

public string email;

public string url;

public DateTime created_at;

public static Image Find(int id)
=> Connection().Query<Image>(
"SELECT * FROM images WHERE id = @id LIMIT 1", new {id}
).FirstOrDefault();

public static Image FindBy(string col, string val)
=> Connection().Query<Image>(
$"SELECT * FROM images WHERE {col} = @val LIMIT 1", new {val}
).FirstOrDefault();

public static int Create(string url) {
return ExecuteScalarInt(
@"INSERT INTO public.images(guid, url) VALUES (@guid, @url); SELECT currval('images_id_seq');"
, new {guid = Guid.NewGuid().ToString(), url}
);
}

public void Delete() => ExecuteScalarInt("DELETE FROM images WHERE id = @id", new {id});
}
}
47 changes: 47 additions & 0 deletions App/DL/Model/Image/Rel/ProjectImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Linq;
using App.DL.Repository.Project;
using Dapper;

namespace App.DL.Model.Image.Rel {
public class ProjectImage : Micron.DL.Model.Model {
public int id;

public string guid;

public int project_id;

public int image_id;

public DateTime created_at;

public static ProjectImage Find(int id)
=> Connection().Query<ProjectImage>(
"SELECT * FROM project_images WHERE id = @id LIMIT 1", new {id}
).FirstOrDefault();

public static ProjectImage[] Get(Project.Project project)
=> Connection().Query<ProjectImage>(
"SELECT * FROM project_images WHERE project_id = @project_id LIMIT 20", new {
project_id = project.id
}
).ToArray();

public static int Create(Project.Project project, Image image) {
return ExecuteScalarInt(
@"INSERT INTO public.project_images(guid, project_id, image_id)
VALUES (@guid, @project_id, @image_id);
SELECT currval('project_images_id_seq');"
, new {
guid = Guid.NewGuid().ToString(), project_id = project.id, image_id = image.id
}
);
}

public Project.Project Project() => ProjectRepository.Find(project_id);

public Image Image() => Model.Image.Image.Find(image_id);

public void Delete() => ExecuteScalarInt("DELETE FROM project_images WHERE id = @id", new {id});
}
}
10 changes: 7 additions & 3 deletions App/DL/Model/Project/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UserModel = App.DL.Model.User.User;
using Dapper;
using App.DL.Model.Alias;
using App.DL.Model.Image.Rel;
using App.DL.Repository.Alias;
using App.DL.Repository.Project;
using App.DL.Repository.Product;
Expand Down Expand Up @@ -57,7 +58,7 @@ public static Project[] GetBy(string col, string val)
=> Connection().Query<Project>(
$"SELECT * FROM projects WHERE {col} = @val LIMIT 50", new {val}
).ToArray();

public static Project[] GetBy(string col, int val)
=> Connection().Query<Project>(
$"SELECT * FROM projects WHERE {col} = @val LIMIT 50", new {val}
Expand Down Expand Up @@ -134,8 +135,11 @@ public bool InLibrary(UserModel user)
, new {project_id = id, user_id = user.id}
) > 0;

public int StarsCount() =>
ExecuteScalarInt("SELECT COUNT(*) FROM user_projects_library WHERE project_id = @id", new {id});
public int StarsCount() => ExecuteScalarInt(
"SELECT COUNT(*) FROM user_projects_library WHERE project_id = @id", new {id}
);

public Image.Image[] Images() => ProjectImage.Get(this).Select(x => x.Image()).ToArray();

public ProjectPost[] Posts() => ProjectPost.Get(this);

Expand Down
15 changes: 15 additions & 0 deletions App/PL/Transformer/Image/ImageTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Micron.PL.Transformer;
using Newtonsoft.Json.Linq;

namespace App.PL.Transformer.Image {
public class ImageTransformer : BaseTransformer {
public override JObject Transform(object obj) {
var item = (DL.Model.Image.Image) obj;
return new JObject {
["guid"] = item.guid,
["url"] = item.url,
["created_at"] = item.created_at,
};
}
}
}
21 changes: 21 additions & 0 deletions migrations/db/migrations/20200131163515_create_images_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php


use Phinx\Migration\AbstractMigration;

class CreateImagesTable extends AbstractMigration
{
public function change()
{
$table = $this->table('images');
$table
->addColumn('guid', 'string')
->addColumn('url', 'string')

->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])

->addIndex(['guid'], ['unique' => true])

->create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php


use Phinx\Migration\AbstractMigration;

class CreateProjectImagesTable extends AbstractMigration
{
public function change()
{
$table = $this->table('project_images');
$table
->addColumn('guid', 'string')
->addColumn('project_id', 'integer', ['signed' => false, 'null' => false])
->addColumn('image_id', 'integer', ['signed' => false, 'null' => false])

->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])

->addIndex(['guid'], ['unique' => true])
->addForeignKey('project_id', 'projects', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
->addForeignKey('image_id', 'images', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])

->create();
}
}