public JsonResult Edit(int blog_seq, string name, string link_url, string info, string photo, string tags, int?important_point, string color) { try { var blog = db.Blog .Where(f => !f.deleted_at.HasValue && f.seq == blog_seq) .FirstOrDefault(); if (blog == null) { throw new HttpException((int)HttpStatusCode.BadRequest, "no blog"); } blog.name = name; blog.link_url = link_url; blog.info = info; blog.tag = tags; blog.important_point = important_point; blog.color = color; blog.updated_at = DateTime.Now; var blog_photos = db.BlogPhoto .Where(bp => !bp.deleted_at.HasValue && bp.blog_seq == blog_seq) .ToList(); // 새로운 사진 추가 if (photo != null) { // 기존 사진 삭제 foreach (var item in blog_photos) { item.deleted_at = DateTime.Now; } var photo_model = new BlogPhoto { blog_seq = blog_seq, created_at = DateTime.Now, photo_url = photo, updated_at = DateTime.Now }; db.BlogPhoto.Add(photo_model); } db.SaveChanges(); var result = new { state = "ok" }; return(Json(result, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); } catch (HttpException e) { Response.StatusCode = e.GetHttpCode(); return(Json(e.Message, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); } }
public List <Blog> SelectAllByUserId() { List <Blog> blogs = null; Blog blog = null; string connectiongString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectiongString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "dbo.Blog_SelectAllByUserId"; SqlParameterCollection paramCollection = cmd.Parameters; paramCollection.AddWithValue("@UseridCreated", UserService.GetCurrentUserId()); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { blog = BlogService.MapBlog(reader); if (blogs == null) { blogs = new List <Blog>(); } blogs.Add(blog); } reader.NextResult(); while (reader.Read()) { int blogId = 0; BlogTag tag = BlogService.MapBlogTag(reader, out blogId); blog = blogs.Find(item => item.Id == blogId); if (blog.Tags == null) { blog.Tags = new List <BlogTag>(); } blog.Tags.Add(tag); } reader.NextResult(); while (reader.Read()) { int blogId = 0; BlogPhoto photo = BlogService.MapBlogPhoto(reader, out blogId); blog = blogs.Find(item => item.Id == blogId); if (blog.Photos == null) { blog.Photos = new List <BlogPhoto>(); } blog.Photos.Add(photo); } } } return(blogs); }
public List <Blog> SelectAll() { List <Blog> blogs = null; string connectiongString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectiongString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "dbo.blog_selectAll"; SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Blog blog = BlogService.MapBlog(reader); if (blogs == null) { blogs = new List <Blog>(); } blogs.Add(blog); } reader.NextResult(); while (reader.Read()) { int blogId = 0; BlogTag tag = BlogService.MapBlogTag(reader, out blogId); Blog d = blogs.Find(item => item.Id == blogId); if (d.Tags == null) { d.Tags = new List <BlogTag>(); } d.Tags.Add(tag); } reader.NextResult(); while (reader.Read()) { int blogId = 0; BlogPhoto photo = BlogService.MapBlogPhoto(reader, out blogId); Blog bl = blogs.Find(item => item.Id == blogId); if (bl.Photos == null) { bl.Photos = new List <BlogPhoto>(); } bl.Photos.Add(photo); } } } return(blogs); }
public List <Blog> SelectAllByUserId() { List <Blog> list = new List <Blog>(); DataProvider.ExecuteCmd(GetConnection, "dbo.Blog_SelectAllByUserId", inputParamMapper : delegate(SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@UseridCreated", UserService.GetCurrentUserId()); }, map : delegate(IDataReader reader, short set) { switch (set) { case 0: Blog b = MapBlog(reader); if (list == null) { list = new List <Blog>(); } list.Add(b); break; case 1: int blogId = 0; BlogTag tag = MapBlogTag(reader, out blogId); Blog d = list.Find(item => item.Id == blogId); if (d.Tags == null) { d.Tags = new List <BlogTag>(); } d.Tags.Add(tag); break; case 2: BlogPhoto photo = MapBlogPhoto(reader, out blogId); Blog bl = list.Find(item => item.Id == blogId); if (bl.Photos == null) { bl.Photos = new List <BlogPhoto>(); } bl.Photos.Add(photo); break; } } ); return(list); }
public List <Blog> SelectAll() { List <Blog> list = new List <Blog>(); DataProvider.ExecuteCmd(GetConnection, "dbo.Blog_SelectAll", inputParamMapper : null, map : delegate(IDataReader reader, short set) { switch (set) { case 0: Blog b = MapBlog(reader); if (list == null) { list = new List <Blog>(); } list.Add(b); break; case 1: int blogId = 0; BlogTag tag = MapBlogTag(reader, out blogId); Blog d = list.Find(item => item.Id == blogId); if (d.Tags == null) { d.Tags = new List <BlogTag>(); } d.Tags.Add(tag); break; case 2: BlogPhoto photo = MapBlogPhoto(reader, out blogId); Blog bl = list.Find(item => item.Id == blogId); if (bl.Photos == null) { bl.Photos = new List <BlogPhoto>(); } bl.Photos.Add(photo); break; } } ); return(list); }
public JsonResult Add(string name, string link_url, string info, string photo, string tags, int?important_point, string color) { try { var new_blog = new Blog { created_at = DateTime.Now, info = info, link_url = link_url, name = name, tag = tags, important_point = important_point.HasValue ? important_point.Value : 0, updated_at = DateTime.Now, color = color }; db.Blog.Add(new_blog); db.SaveChanges(); if (photo != null) { var new_blog_photo = new BlogPhoto { blog_seq = new_blog.seq, created_at = DateTime.Now, photo_url = photo, updated_at = DateTime.Now }; db.BlogPhoto.Add(new_blog_photo); } db.SaveChanges(); var result = new { state = "ok" }; return(Json(result, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); } catch (HttpException e) { Response.StatusCode = e.GetHttpCode(); return(Json(e.Message, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); } }
public Blog SelectById(int id) { Blog blog = null; DataProvider.ExecuteCmd(GetConnection, "dbo.Blog_SelectById", inputParamMapper : delegate(SqlParameterCollection paramCollection) { paramCollection.AddWithValue("@id", id); } , map : delegate(IDataReader reader, short set) { switch (set) { case 0: blog = MapBlog(reader); break; case 1: int blogId = 0; BlogTag tag = MapBlogTag(reader, out blogId); if (blog.Tags == null) { blog.Tags = new List <BlogTag>(); } blog.Tags.Add(tag); break; case 2: // int blogId = 0; BlogPhoto photo = MapBlogPhoto(reader, out blogId); if (blog.Photos == null) { blog.Photos = new List <BlogPhoto>(); } blog.Photos.Add(photo); break; } } ); return(blog); }
public async Task <EntityResponse <PhotoDTO> > CreateAsync(PhotoDTO dto, Guid blogId) { var blog = await DbSet <Blog>().FindAsync(blogId).ConfigureAwait(false); if (blog == null) { return(new EntityResponse <PhotoDTO>($"The entity {nameof(Blog).ToLower()} could not be found with id: {blogId}")); } return((EntityResponse <PhotoDTO>) await TransactionAsync(async context => { var photo = Mapper.Map <Photo>(dto); try { photo.ImageUrl = (await _storageProvider.SaveFileAndGetUriAsync(ContainerStore.PHOTO_CONTAINER, dto.ImageFile.FileName, dto.ImageFile).ConfigureAwait(false)).ToString(); photo.FileName = dto.ImageFile.FileName; await context.Photos.AddAsync(photo).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); var blogPhoto = new BlogPhoto { BlogId = blog.Id, PhotoId = photo.Id }; await context.Set <BlogPhoto>().AddAsync(blogPhoto).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); return new EntityResponse <PhotoDTO>(Mapper.Map <PhotoDTO>(photo)); } catch { await _storageProvider.DeleteAsync(ContainerStore.PHOTO_CONTAINER, photo.FileName).ConfigureAwait(false); throw; } }).ConfigureAwait(false)); }