public LacesResponse AddTag(AddTagRequest request) { LacesResponse response = new LacesResponse(); try { if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN]) { LacesDataModel.Product.Product product = new LacesDataModel.Product.Product(request.ProductId); // Verify product exists LacesDataModel.Product.Tag tag = new LacesDataModel.Product.Tag(); tag.ProductId = product.ProductId; tag.Description = request.Description; if (tag.Add()) { response.Success = true; response.Message = "Tag succesfully added."; } else { response.Success = false; response.Message = "An error occurred when communicating with the database."; } } else { response.Success = false; response.Message = "Invalid security token."; } } catch (Exception ex) { response = new LacesResponse(); response.Success = false; if (ex.Message.Contains("find product")) { response.Message = ex.Message; } else { response.Message = "An unexpected error has occurred; please verify the format of your request."; } } return(response); }
public AddProductResponse AddProduct(AddProductRequest request) { AddProductResponse response = new AddProductResponse(); try { if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN]) { LacesDataModel.User.User user = new LacesDataModel.User.User(request.UserId); // Ensure user exists. LacesDataModel.Product.Product product = new LacesDataModel.Product.Product(); product.AskingPrice = request.AskingPrice; product.Brand = request.Brand; product.ConditionId = request.ConditionId; product.CreatedDate = DateTime.Now; product.Description = request.Description; product.Name = request.ProductName; product.ProductStatusId = (int)ProductStatusOptions.Open; product.ProductTypeId = request.ProductTypeId; product.SellerId = user.UserId; product.Size = request.Size; product.UpdatedDate = DateTime.Now; if (product.Add()) { if (request.Images != null && request.Images.Count > 0) { int count = 0; /* This is needed until we can get the Id back from Add(). Remove ASAP */ List <LacesDataModel.Product.Product> queryResult = new List <LacesDataModel.Product.Product>(); SearchEntity search = new SearchEntity(); search.ColumnsToReturn = new List <string>(); search.ColumnsToReturn.Add("ProductId"); search.ConnectionString = Constants.CONNECTION_STRING; search.OrderBy = new OrderBy(); search.OrderBy.Column = "ProductId"; search.OrderBy.Direction = OrderByDirection.DESC; search.PageSizeLimit = 1; search.SchemaName = Constants.SCHEMA_DEFAULT; search.TableName = Constants.TABLE_PRODUCTS; queryResult = new GenericRepository <LacesDataModel.Product.Product>().Read(search); if (queryResult.Count > 0) { product.ProductId = queryResult[0].ProductId; } /* End remove section */ foreach (LacesAPI.Models.Request.ImageInfo image in request.Images) { string fileName = product.ProductId + "_" + count + "." + image.FileFormat; string filePath = ConfigurationManager.AppSettings[Constants.APP_SETTING_PRODUCT_IMAGE_DIRECTORY] + fileName; File.WriteAllBytes(filePath, image.ImageData); Image productImage = new Image(); productImage.AssociatedEntityId = product.ProductId; productImage.FileFormat = image.FileFormat; productImage.FileName = fileName; productImage.FilePath = filePath; productImage.ImageEntityTypeId = (int)ImageEntityTypeOptions.Product; productImage.CreatedDate = DateTime.Now; productImage.UpdatedDate = DateTime.Now; productImage.Add(); } } if (request.Tags != null && request.Tags.Count > 0) { foreach (string tag in request.Tags) { LacesDataModel.Product.Tag newTag = new LacesDataModel.Product.Tag(); newTag.ProductId = product.ProductId; newTag.Description = tag; newTag.Add(); } } response.Success = true; response.Message = "Product succesfully created."; } else { response.Success = false; response.Message = "An error occurred when communicating with the database."; } } else { response.Success = false; response.Message = "Invalid security token."; } } catch (Exception ex) { response = new AddProductResponse(); response.Success = false; if (ex.Message.Contains("find user")) { response.Message = ex.Message; } else { response.Message = "An unexpected error has occurred; please verify the format of your request."; } } return(response); }