From e1a8670392451c20b9c18730773ee42b7a38e9df Mon Sep 17 00:00:00 2001 From: urlilpolly Date: Tue, 14 May 2024 17:48:22 +0400 Subject: [PATCH] full database --- SUBD_LABA/Database/Abstracts.cs | 52 +++ SUBD_LABA/Database/Database.csproj | 13 + SUBD_LABA/Database/Implements.cs | 523 +++++++++++++++++++++++++++++ SUBD_LABA/Database/Models.cs | 47 +++ SUBD_LABA/SUBD_LAB.sln | 25 ++ 5 files changed, 660 insertions(+) create mode 100644 SUBD_LABA/Database/Abstracts.cs create mode 100644 SUBD_LABA/Database/Database.csproj create mode 100644 SUBD_LABA/Database/Implements.cs create mode 100644 SUBD_LABA/Database/Models.cs create mode 100644 SUBD_LABA/SUBD_LAB.sln diff --git a/SUBD_LABA/Database/Abstracts.cs b/SUBD_LABA/Database/Abstracts.cs new file mode 100644 index 0000000..79ccd37 --- /dev/null +++ b/SUBD_LABA/Database/Abstracts.cs @@ -0,0 +1,52 @@ +using Database; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Commentbase +{ + public abstract class Abstracts + { + public abstract void CreateAlbum(Album album); + public abstract void UpdateAlbum(Album album); + public abstract void DeleteAlbum(int id); + public abstract Album GetAlbum(int id); + public abstract Album GetAlbum(string title); + public abstract List GetAlbums(); + public abstract void DeleteAllAlbums(); + + public abstract void CreateLocation(Location location); + public abstract void UpdateLocation(Location location); + public abstract void DeleteLocation(int id); + public abstract Location GetLocation(int id); + public abstract Location GetLocation(string name); + public abstract List GetLocations(); + public abstract void DeleteAllLocations(); + + public abstract void CreatePhoto(Photo photo); + public abstract void UpdatePhoto(Photo photo); + public abstract void DeletePhoto(int id); + public abstract Photo GetPhoto(int id); + public abstract Photo GetPhoto(int AlbumId, int LocationId, int AuthorId); + public abstract List GetPhotos(); + public abstract void DeleteAllPhotos(); + + public abstract void CreateAuthor(Author author); + public abstract void UpdateAuthor(Author author); + public abstract void DeleteAuthor(int id); + public abstract Author GetAuthor(int id); + public abstract Author GetAuthor(string name); + public abstract List GetAuthors(); + public abstract void DeleteAllAuthors(); + + public abstract void CreateComment(Comment comment); + public abstract void UpdateComment(Comment comment); + public abstract void DeleteComment(int id); + public abstract Comment GetComment(int id); + public abstract Comment GetCommentPhoto(int PhotoId); + public abstract List GetComments(); + public abstract void DeleteAllComments(); + } +} diff --git a/SUBD_LABA/Database/Database.csproj b/SUBD_LABA/Database/Database.csproj new file mode 100644 index 0000000..176d47a --- /dev/null +++ b/SUBD_LABA/Database/Database.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/SUBD_LABA/Database/Implements.cs b/SUBD_LABA/Database/Implements.cs new file mode 100644 index 0000000..e1b647c --- /dev/null +++ b/SUBD_LABA/Database/Implements.cs @@ -0,0 +1,523 @@ +using Commentbase; +using Npgsql; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Database +{ + public class Implements : Abstracts + { + private NpgsqlConnection GetConnect() + { + return new NpgsqlConnection("Host=192.168.56.101;Username=postgres;Password=postgres;Database=calendar"); + } + public override void CreateAlbum(Album album) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("INSERT INTO Album (Title, Description) " + + "VALUES (@Title, @Description)", conn); + cmd.Parameters.AddWithValue("@Title", album.Title); + cmd.Parameters.AddWithValue("@Description", album.Description); + cmd.ExecuteNonQuery(); + } + + public override void UpdateAlbum(Album album) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"UPDATE Album SET Title = @Title, Description = @Description WHERE id = @Id", conn); + cmd.Parameters.AddWithValue("@Title", album.Title); + cmd.Parameters.AddWithValue("@Description", album.Description); + cmd.Parameters.AddWithValue("@Id", album.Id); + cmd.ExecuteNonQuery(); + } + + public override void DeleteAlbum(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Album WHERE id = @Id", conn); + cmd.Parameters.AddWithValue("@Id", id); + cmd.ExecuteNonQuery(); + } + + public override Album GetAlbum(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Album WHERE id = {id}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Album + { + Id = reader.GetInt32(0), + Title = reader.GetString(1), + Description = reader.GetString(2), + }; + } + return null; + } + + public override Album GetAlbum(string title) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Album WHERE Title = {title}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Album + { + Id = reader.GetInt32(0), + Title = reader.GetString(1), + Description = reader.GetString(2), + }; + } + return null; + } + + public override List GetAlbums() + { + List albums = new List(); + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("SELECT * FROM Album", conn); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + albums.Add(new Album + { + Id = reader.GetInt32(0), + Title = reader.GetString(1), + Description = reader.GetString(2), + }); + } + return albums; + } + + public override void DeleteAllAlbums() + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Album", conn); + cmd.ExecuteNonQuery(); + } + + public override void CreateLocation(Location location) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("INSERT INTO Location (Name, Shortname) " + + "VALUES (@Name, @Shortname)", conn); + cmd.Parameters.AddWithValue("@Name", location.Name); + cmd.Parameters.AddWithValue("@Shortname", location.ShortName); + cmd.ExecuteNonQuery(); + } + + public override void UpdateLocation(Location location) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("UPDATE Location SET Name = @Name, ShortName = @Shortname WHERE Id = @Id", conn); + cmd.Parameters.AddWithValue("@Name", location.Name); + cmd.Parameters.AddWithValue("@Shortname", location.ShortName); + cmd.ExecuteNonQuery(); + } + + public override void DeleteLocation(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Location WHERE id = @Id", conn); + cmd.Parameters.AddWithValue("@Id", id); + cmd.ExecuteNonQuery(); + } + + public override Location GetLocation(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Location WHERE id = {id}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Location + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + ShortName = reader.GetString(2), + }; + } + return null; + } + + public override Location GetLocation(string name) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Location WHERE Name = {name}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Location + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + ShortName = reader.GetString(2), + }; + } + return null; + } + + public override List GetLocations() + { + List locations = new List(); + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("SELECT * FROM Location", conn); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + locations.Add(new Location + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + ShortName = reader.GetString(2), + }); + } + return locations; + } + + public override void DeleteAllLocations() + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Location", conn); + cmd.ExecuteNonQuery(); + } + + public override void CreatePhoto(Photo photo) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("INSERT INTO Photo (Title, Description, Privacy, UploadDate, ImagePath, AlbumId, LocationId, AuthorId) " + + "VALUES (@Title, @Description, @Privacy, @UploadDate, @ImagePath, @Album, @Location, @Author)", conn); + cmd.Parameters.AddWithValue("@Title", photo.Title); + cmd.Parameters.AddWithValue("@Description", photo.Description); + cmd.Parameters.AddWithValue("@Privacy", photo.Privacy); + cmd.Parameters.AddWithValue("@UploadDate", photo.UploadDate); + cmd.Parameters.AddWithValue("@ImagePath", photo.ImagePath); + cmd.Parameters.AddWithValue("@Album", photo.AlbumId); + cmd.Parameters.AddWithValue("@Location", photo.LocationId); + cmd.Parameters.AddWithValue("@Author", photo.AuthorId); + cmd.ExecuteNonQuery(); + } + + public override void UpdatePhoto(Photo photo) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("UPDATE Photo SET Title = @Title, Description = @Description, Privacy = @Privacy, UploadDate = @UploadDate, ImagePath = @ImagePath, AlbumId = @Album, LocationId = @Location, AuthorId = @Author", conn); + cmd.Parameters.AddWithValue("@Title", photo.Title); + cmd.Parameters.AddWithValue("@Description", photo.Description); + cmd.Parameters.AddWithValue("@Privacy", photo.Privacy); + cmd.Parameters.AddWithValue("@UploadDate", photo.UploadDate); + cmd.Parameters.AddWithValue("@ImagePath", photo.ImagePath); + cmd.Parameters.AddWithValue("@Album", photo.AlbumId); + cmd.Parameters.AddWithValue("@Location", photo.LocationId); + cmd.Parameters.AddWithValue("@Author", photo.AuthorId); + cmd.ExecuteNonQuery(); + } + + public override void DeletePhoto(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Photo WHERE id = @Id", conn); + cmd.Parameters.AddWithValue("@Id", id); + cmd.ExecuteNonQuery(); + } + + public override Photo GetPhoto(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Photo WHERE id = {id}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Photo + { + Id = reader.GetInt32(0), + Title = reader.GetString(1), + Description = reader.GetString(2), + Privacy = reader.GetString(3), + UploadDate = reader.GetDateTime(4), + ImagePath = reader.GetString(5), + AlbumId = reader.GetInt32(6), + LocationId = reader.GetInt32(7), + AuthorId = reader.GetInt32(8) + }; + } + return null; + } + + public override Photo GetPhoto(int AlbumId, int LocationId, int AuthorId) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Photo WHERE (AlbumId = {AlbumId} AND LocationId = {LocationId} AND AuthorId = {AuthorId})", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Photo + { + Id = reader.GetInt32(0), + Title = reader.GetString(1), + Description = reader.GetString(2), + Privacy = reader.GetString(3), + UploadDate = reader.GetDateTime(4), + ImagePath = reader.GetString(5), + AlbumId=reader.GetInt32(6), + LocationId = reader.GetInt32(7), + AuthorId = reader.GetInt32(8) + }; + } + return null; + } + + public override List GetPhotos() + { + List photos = new List(); + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("SELECT * FROM Photo", conn); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + photos.Add(new Photo + { + Id = reader.GetInt32(0), + Title = reader.GetString(1), + Description = reader.GetString(2), + Privacy = reader.GetString(3), + UploadDate = reader.GetDateTime(4), + ImagePath = reader.GetString(5), + AlbumId = reader.GetInt32(6), + LocationId = reader.GetInt32(7), + AuthorId = reader.GetInt32(8) + }); + } + return photos; + } + + public override void DeleteAllPhotos() + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Photo", conn); + cmd.ExecuteNonQuery(); + } + + public override void CreateAuthor(Author author) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("INSERT INTO Author (Name, PhoneNum, Email) " + + "VALUES (@Name, @PhoneNum, @Email)", conn); + cmd.Parameters.AddWithValue("@Name", author.Name); + cmd.Parameters.AddWithValue("@PhoneNum", author.PhoneNum); + cmd.Parameters.AddWithValue("@Email", author.Email); + cmd.ExecuteNonQuery(); + } + + public override void UpdateAuthor(Author author) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("UPDATE Author SET Name = @Name, PhoneNum = @PhoneNum, Email = @Email", conn); + cmd.Parameters.AddWithValue("@Name", author.Name); + cmd.Parameters.AddWithValue("@PhoneNum", author.PhoneNum); + cmd.Parameters.AddWithValue("@Email", author.Email); + cmd.ExecuteNonQuery(); + } + + public override void DeleteAuthor(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Author WHERE id = @Id", conn); + cmd.Parameters.AddWithValue("@Id", id); + cmd.ExecuteNonQuery(); + } + + public override Author GetAuthor(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Author WHERE id = {id}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Author + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + PhoneNum = reader.GetString(2), + Email = reader.GetString(3) + }; + } + return null; + } + + public override Author GetAuthor(string name) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Author WHERE Name = {name}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Author + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + PhoneNum = reader.GetString(2), + Email = reader.GetString(3) + }; + } + return null; + } + + public override List GetAuthors() + { + List authors = new List(); + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("SELECT * FROM Author", conn); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + authors.Add(new Author + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + PhoneNum = reader.GetString(2), + Email = reader.GetString(3) + }); + } + return authors; + } + + public override void DeleteAllAuthors() + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Author", conn); + cmd.ExecuteNonQuery(); + } + + public override void CreateComment(Comment comment) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("INSERT INTO Comment (PostDate, Content, PhotoId) " + + "VALUES (@PostDate, @Content, @Photo)", conn); + cmd.Parameters.AddWithValue("@PostDate", comment.PostDate); + cmd.Parameters.AddWithValue("@Content", comment.Content); + cmd.Parameters.AddWithValue("@Photo", comment.PhotoId); + cmd.ExecuteNonQuery(); + } + + public override void UpdateComment(Comment comment) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("UPDATE Comment SET PostDate = @PostDate, Content = @Content, PhotoId = @Photo", conn); + cmd.Parameters.AddWithValue("@PostDate", comment.PostDate); + cmd.Parameters.AddWithValue("@Content", comment.Content); + cmd.Parameters.AddWithValue("@Photo", comment.PhotoId); + cmd.ExecuteNonQuery(); + } + + public override void DeleteComment(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Comment WHERE id = @Id", conn); + cmd.Parameters.AddWithValue("@Id", id); + cmd.ExecuteNonQuery(); + } + + public override Comment GetComment(int id) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Comment WHERE id = {id}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Comment + { + Id = reader.GetInt32(0), + PostDate = reader.GetDateTime(1), + Content = reader.GetString(2), + PhotoId = reader.GetInt32(3) + }; + } + return null; + } + + public override Comment GetCommentPhoto(int PhotoId) + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"SELECT * FROM Comment WHERE PhotoId = {PhotoId}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Comment + { + Id = reader.GetInt32(0), + PostDate = reader.GetDateTime(1), + Content = reader.GetString(2), + PhotoId = reader.GetInt32(3) + }; + } + return null; + } + + public override List GetComments() + { + List comments = new List(); + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand("SELECT * FROM Comment", conn); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + comments.Add(new Comment + { + Id = reader.GetInt32(0), + PostDate = reader.GetDateTime(1), + Content = reader.GetString(2), + PhotoId = reader.GetInt32(3) + }); + } + return comments; + } + + public override void DeleteAllComments() + { + using var conn = GetConnect(); + conn.Open(); + using var cmd = new NpgsqlCommand($"DELETE FROM Comment", conn); + cmd.ExecuteNonQuery(); + } + } +} diff --git a/SUBD_LABA/Database/Models.cs b/SUBD_LABA/Database/Models.cs new file mode 100644 index 0000000..6bd4c44 --- /dev/null +++ b/SUBD_LABA/Database/Models.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Database +{ + public class Album + { + public int Id { get; set; } + public string Title { get; set; } + public string Description { get; set; } + } + public class Location + { + public int Id { get; set; } + public string Name { get; set; } + public string ShortName { get; set; } + } + public class Author + { + public int Id { get; set; } + public string Name { get; set; } + public string PhoneNum { get; set; } + public string Email { get; set; } + } + public class Photo + { + public int Id { get; set; } + public string Title { get; set; } + public string Description { get; set; } + public string Privacy { get; set; } + public DateTime UploadDate { get; set; } + public string ImagePath { get; set; } + public int AlbumId { get; set; } + public int LocationId { get; set; } + public int AuthorId { get; set; } + } + public class Comment + { + public int Id { get; set; } + public DateTime PostDate { get; set; } + public string Content { get; set; } + public int PhotoId { get; set; } + } +} diff --git a/SUBD_LABA/SUBD_LAB.sln b/SUBD_LABA/SUBD_LAB.sln new file mode 100644 index 0000000..e9b6012 --- /dev/null +++ b/SUBD_LABA/SUBD_LAB.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33801.468 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Database", "Database\Database.csproj", "{7D559A78-17F8-4ADB-BA91-30D71CE60CC6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7D559A78-17F8-4ADB-BA91-30D71CE60CC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D559A78-17F8-4ADB-BA91-30D71CE60CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D559A78-17F8-4ADB-BA91-30D71CE60CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D559A78-17F8-4ADB-BA91-30D71CE60CC6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {21800503-431A-49CE-A6F1-523F96E07432} + EndGlobalSection +EndGlobal