Permalink
Browse files

merge fix

  • Loading branch information...
2 parents 739600b + 4f1cab1 commit fc4d790b1ce75220f33db64204e0fb119555d779 @Martikos Martikos committed Apr 24, 2013
@@ -20,8 +20,8 @@ public static Listing getListing(String id)
cmd.Parameters.AddWithValue("@ListingId", id);
SqlDataReader reader = cmd.ExecuteReader();
- reader.Read();
- Listing returnListing = extractListing(reader);
+ Listing returnListing = null;
+ if(reader.Read()) returnListing = extractListing(reader);
conn.Close();
return returnListing;
}
@@ -116,15 +116,15 @@ public static Boolean updateListing(String idToUpdate, Listing newListing)
{
SqlConnection conn = DBConnector.getSqlConnection();
conn.Open();
- SqlCommand cmd = new SqlCommand("UPDATE Listing SET UserId = @UserId, Title = @Title, Description = @Description, Price = @Price, Location = @Location, Date = @Date WHERE ListingId = @ListingId");
- cmd.Parameters.AddWithValue("@UserId", newListing.userId);
+ SqlCommand cmd = new SqlCommand("UPDATE Listing SET Title = @Title, Description = @Description, Price = @Price, Location = @Location, Date = @Date, Image = @Image WHERE ListingId = @ListingId", conn);
cmd.Parameters.AddWithValue("@Title", newListing.title);
cmd.Parameters.AddWithValue("@Description", newListing.description);
cmd.Parameters.AddWithValue("@Price", newListing.price);
cmd.Parameters.AddWithValue("@Location", newListing.location);
cmd.Parameters.AddWithValue("@Date", newListing.date);
+ cmd.Parameters.AddWithValue("@Image", newListing.imageId);
cmd.Parameters.AddWithValue("@ListingId", idToUpdate);
-
+
int rowsAffected = cmd.ExecuteNonQuery();
conn.Close();
@@ -160,6 +160,17 @@ public static void addListingTag(Listing listing, Tag tag)
conn.Close();
}
+ public static void deleteListingTag(Listing listing, Tag tag)
+ {
+ SqlConnection conn = DBConnector.getSqlConnection();
+ conn.Open();
+ SqlCommand cmd = new SqlCommand("DELETE FROM ListingTags WHERE ListingId = @ListingID AND TagId = @TagId", conn);
+ cmd.Parameters.AddWithValue("@ListingId", listing.ListingId);
+ cmd.Parameters.AddWithValue("@TagId", tag.id);
+ cmd.ExecuteNonQuery();
+ conn.Close();
+ }
+
@@ -34,6 +34,40 @@ public static Tag createNewTag(string name)
return new Tag(id, name);
}
+ public static List<Tag> getTagsFromListing(Listing listing)
+ {
+ SqlConnection conn = DBConnector.getSqlConnection();
+ conn.Open();
+ SqlCommand cmd = new SqlCommand("SELECT TagId FROM ListingTags where ListingId = @ListingId", conn);
+ cmd.Parameters.AddWithValue("@ListingId", listing.ListingId);
+ SqlDataReader reader = cmd.ExecuteReader();
+
+ List<Tag> tags = new List<Tag>();
+
+ while (reader.Read())
+ {
+ int tagId = (int) reader["TagId"];
+ tags.Add(new Tag(tagId));
+ }
+ cmd.Dispose();
+ reader.Dispose();
+
+ string selectStr = "SELECT Name FROM Tags where TagId = @TagId";
+
+ foreach (Tag t in tags) {
+ SqlCommand getName = new SqlCommand(selectStr, conn);
+ getName.Parameters.AddWithValue("@TagId", t.id);
+ SqlDataReader nameReader = getName.ExecuteReader();
+ if (nameReader.Read())
+ {
+ t.name = (string) nameReader["Name"];
+ }
+ getName.Dispose();
+ nameReader.Dispose();
+ }
+ return tags;
+ }
+
public static Tag getTag(int id)
{
SqlConnection conn = DBConnector.getSqlConnection();
View
@@ -15,5 +15,11 @@ public Tag(int id, string name)
{
this.id = id;
this.name = name;
- }
+ }
+
+ public Tag(int id)
+ {
+ this.id = id;
+ name = "";
+ }
}
View
@@ -6,6 +6,7 @@
using System.Web.UI.WebControls;
using System.Web.Security;
+
public partial class Views_MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
@@ -30,8 +31,18 @@ protected void go_post(object sender, EventArgs e)
Response.Redirect("~/Views/Private/Post.aspx");
}
protected void go_profile(object sender, EventArgs e)
- {
- Response.Redirect("~/Views/Private/Profile.aspx");
+ {
+ if (HttpContext.Current.User.Identity.IsAuthenticated)
+ {
+ MembershipUser userInfo = Membership.GetUser();
+ Guid userId = (Guid)userInfo.ProviderUserKey;
+ Response.Redirect("~/Views/Search.aspx?user=" + userId.ToString());
+ }
+ else
+ {
+ Response.Redirect("~/Views/Login.aspx");
+ }
+
}
protected void go_notifications(object sender, EventArgs e)
{
@@ -44,7 +44,7 @@ protected void Button1_Click(object sender, EventArgs e)
Listing newListing = ListingDataService.addListing(listing);
/* save the tags along with the listing id */
- string[] words = textbox_tags.Text.Split(' ');
+ string[] words = textbox_tags.Text.Trim().Split(' ');
for (int i = 0; i < words.Length; i++ )
{
Tag newTag = TagDataService.createNewTag(words[i]);
@@ -0,0 +1,48 @@
+<%@ Page Language="C#" MasterPageFile="~/Views/Layout.master" AutoEventWireup="true" CodeFile="UpdatePost.aspx.cs" Inherits="Views_Private_UpdatePost" %>
+
+<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
+ <script type="text/javascript">
+ function changeImagePreview(uploadControl) {
+ if (uploadControl.files && uploadControl.files[0]) {
+ var fileReader = new FileReader();
+ fileReader.onload = function (x) {
+ document.getElementById('MainContent_image_preview').setAttribute('src', x.target.result);
+ };
+ fileReader.readAsDataURL(uploadControl.files[0]);
+ } else {
+ var id = document.getElementById('MainContent_original_imageid').getAttribute('Value');
+ document.getElementById('MainContent_image_preview').setAttribute('src', "../../Helpers/GetImage.ashx?ID=" + id);
+ }
+ }
+ </script>
+ <div id="form_item">
+ <table align="center">
+ <tr>
+ <td>Listing: </td>
+ <td><asp:Label ID="listing_id" runat="server"></asp:Label></td>
+ </tr>
+ <tr>
+ <td>
+ <fieldset>
+ <asp:HiddenField ID="original_imageid" runat="server" />
+ <label for="Title" class="form_label">Title</label><asp:TextBox ID="textbox_title" runat="server" name="Title" class="form_field"></asp:TextBox><br /><br />
+ <label for="Description" class="form_label">Description</label><textarea id="textbox_description" runat="server" name="Description" class="form_field"></textarea><br /><br />
+ <label for="Price" class="form_label">Price</label><asp:TextBox ID="textbox_price" runat="server" name="Price" class="form_field"></asp:TextBox><br /><br />
+ <label for="Location" class="form_label">Location</label><asp:TextBox ID="textbox_location" runat="server" name="Location" class="form_field"></asp:TextBox><br /><br />
+ <label for="Tags" class="form_label">Tags</label><asp:TextBox ID="textbox_tags" runat="server" name="Tags" class="form_field"></asp:TextBox><br /><br />
+ <label for="Image" class="form_label">Image (Optional)</label><asp:FileUpload ID="imageUpload" runat="server" onchange="changeImagePreview(this)"/><br /><br />
+ <asp:Label ID="addlisting_output" runat="server" Text=""></asp:Label><br />
+ <label for="Tags" class="form_label"></label><asp:Button ID="button_post" runat="server" Text="Update Listing" class="form_button" OnClick="Update_Click"/>
+
+ </fieldset>
+ </td>
+ <td style="background-color: #eee; width: 200px; height: 100px" >
+ <asp:Image ID="image_preview" runat="server" Width="200" />
+ </td>
+ </tr>
+ </table>
+
+ </div>
+
+
+</asp:Content>
@@ -0,0 +1,192 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.UI;
+using System.Web.UI.WebControls;
+using System.Web.Security;
+using System.IO;
+using System.Drawing;
+
+public partial class Views_Private_UpdatePost : System.Web.UI.Page
+{
+ protected void Page_Load(object sender, EventArgs e)
+ {
+ if (!IsPostBack)
+ {
+ if (string.IsNullOrEmpty(Request.QueryString["L"]))
+ {
+ Response.Redirect("~/Views/Error.aspx?e_code=404");
+ return;
+ }
+
+ int listingId = 0;
+ try
+ {
+ listingId = Convert.ToInt32(Request.QueryString["L"]);
+ }
+ catch
+ {
+ Response.Redirect("~/Views/Error.aspx?e_code=400");
+ return;
+ }
+
+ Listing listing = ListingDataService.getListing(listingId.ToString());
+ if (listing == null)
+ {
+ Response.Redirect("~/Views/Error.aspx?e_code=404");
+ return;
+ }
+
+ original_imageid.Value = listing.imageId.ToString();
+ textbox_title.Text = listing.title;
+ textbox_description.Value = listing.description;
+ textbox_price.Text = listing.price.ToString();
+ textbox_location.Text = listing.location;
+ List<Tag> tags = TagDataService.getTagsFromListing(listing);
+ string tag_string = "";
+ foreach (Tag t in tags)
+ {
+ tag_string += t.name + " ";
+ }
+
+ textbox_tags.Text = tag_string;
+
+ image_preview.ImageUrl = "~/Helpers/GetImage.ashx?ID=" + listing.imageId;
+ }
+
+ }
+ protected void Update_Click(object sender, EventArgs e)
+ {
+ if (textbox_title.Text != ""
+ && textbox_price.Text != ""
+ && textbox_description.Value != ""
+ && textbox_location.Text != ""
+ && textbox_tags.Text != "")
+ {
+ string title = textbox_title.Text;
+ decimal price = 0;
+ try
+ {
+ price = Convert.ToDecimal(textbox_price.Text);
+ string description = textbox_description.Value.ToString();
+ string location = textbox_location.Text;
+ string tags = textbox_tags.Text;
+
+
+ Listing oldListing = ListingDataService.getListing(Request.QueryString["L"]);
+ oldListing.title = title;
+ oldListing.description = description;
+ oldListing.price = price;
+ oldListing.location = location;
+
+ if (imageUpload.HasFile)
+ {
+ int oldImageId = oldListing.imageId;
+ oldListing.imageId = saveImageFile();
+ ImageDataService.deleteImage(oldImageId);
+ }
+
+ bool success = ListingDataService.updateListing(oldListing.ListingId.ToString(), oldListing);
+
+ List<Tag> oldTags = TagDataService.getTagsFromListing(oldListing);
+ List<String> oldTagNames = oldTags.Select(g => g.name).ToList();
+ /* save the tags along with the listing id */
+ string[] words = textbox_tags.Text.Trim().Split(' ');
+ foreach (string word in words)
+ {
+ if(oldTagNames.Contains(word)) {
+ oldTagNames.Remove(word);
+ continue;
+ }
+ Tag newTag = TagDataService.createNewTag(word);
+ ListingDataService.addListingTag(oldListing, newTag);
+ }
+
+ foreach (string tagName in oldTagNames)
+ {
+ Tag tag = oldTags.First(t => t.name == tagName);
+ ListingDataService.deleteListingTag(oldListing, tag);
+ }
+
+ addlisting_output.Text = "Listing updated successfully!";
+ addlisting_output.Style.Add("color", "#00ff00");
+ }
+ catch (Exception ex)
+ {
+
+ addlisting_output.Style.Add("color", "#ff0000");
+ if (ex is OverflowException)
+ {
+ addlisting_output.Text = "Please enter a smaller price value.";
+ }
+ else if (ex is FormatException)
+ {
+ addlisting_output.Text = "Please enter a valid price value.";
+ }
+ else
+ {
+ addlisting_output.Text = ex.Message;
+ }
+ }
+
+ }
+ else
+ {
+ /* Please fill all fields */
+ addlisting_output.Text = "Please fill all of the fields.";
+ addlisting_output.Style.Add("color", "#ff0000");
+
+ }
+ }
+
+ private int saveImageFile()
+ {
+ string ext = Path.GetExtension(imageUpload.PostedFile.FileName).ToLower();
+ string contentType = "";
+ switch (ext)
+ {
+ case ".jpg":
+ case ".jpeg":
+ contentType = "image/jpg";
+ break;
+ case ".png":
+ contentType = "image/png";
+ break;
+ case ".gif":
+ contentType = "image/gif";
+ break;
+ case ".bmp":
+ contentType = "image/bmp";
+ break;
+ }
+
+ Stream stream = imageUpload.PostedFile.InputStream;
+ BinaryReader binReader = new BinaryReader(stream);
+ byte[] data = binReader.ReadBytes(Convert.ToInt32(stream.Length));
+ System.Drawing.Image temp = System.Drawing.Image.FromStream(stream);
+ int width = (int)temp.Width;
+ int height = (int)temp.Height;
+ temp.Dispose();
+
+ int thumbWidth = 100;
+ int thumbHeight = thumbWidth * (int)((double)width / (double)height);
+
+ System.Drawing.Image thumb = System.Drawing.Image.FromStream(stream);
+ Bitmap tempThumb = new Bitmap(thumb, thumbWidth, thumbHeight);
+ Graphics g = Graphics.FromImage(tempThumb);
+ g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
+ g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
+ g.DrawImage(thumb, 0, 0, tempThumb.Width, tempThumb.Height);
+ MemoryStream memStream = new MemoryStream();
+ tempThumb.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
+ byte[] thumbData = memStream.ToArray();
+
+ thumb.Dispose();
+ tempThumb.Dispose();
+ g.Dispose();
+ Image fullImage = new Image(data, contentType, height, width);
+ Image thumbImage = new Image(thumbData, "image/jpg", thumbHeight, thumbWidth);
+ return ImageDataService.addImage(fullImage, thumbImage);
+ }
+}
@@ -10,10 +10,12 @@
};
fileReader.readAsDataURL(uploadControl.files[0]);
} else {
- document.getElementById('MainContent_user_photo').setAttribute('src', "../../public/img/grey_wash_wall.png");
+ var id = document.getElementById('MainContent_original_image_id').getAttribute('Value');
+ document.getElementById('MainContent_user_photo').setAttribute('src', "../../Helpers/GetImage.ashx?ID=" + id);
}
}
</script>
+ <asp:HiddenField ID="original_image_id" runat="server" />
Hello <asp:LoginName ID="LoginName1" runat="server" />!<br /><br />
<div style="text-align: center">
<table align="center">
Oops, something went wrong.

0 comments on commit fc4d790

Please sign in to comment.