public IHttpActionResult Register([FromBody] RegisterViewModel model)
        {
            if (model == null)
            {
                return(BadRequest("An error ocured while trying to register to Wishlist, please try again later."));
            }

            if (_db.Users.Any(u => !u.IsDeleted && u.Username.Equals(model.Username)))
            {
                return(BadRequest($"Username \"{model.Username}\" is already taken."));
            }

            Users user = model;

            try
            {
                _db.Users.Add(user);
                _db.SaveChanges();
            }
            catch (Exception)
            {
                return(BadRequest("An error ocured while trying to register to Wishlist, please try again later."));
            }

            return(Ok());
        }
示例#2
0
        public IHttpActionResult Edit([FromBody] Payments model)
        {
            if (model == null)
            {
                return(BadRequest("An error occurred while trying to edit payment, please try again later"));
            }

            var authToken   = Request.Headers.GetValues("authToken").FirstOrDefault()?.ToString() ?? null;
            var authTokenDb = _db.AuthenticationTokens.FirstOrDefault(at => at.Token.Equals(authToken));

            if (!authTokenDb?.IsTokenValid(authToken) ?? true)
            {
                return(Unauthorized());
            }

            var payment = _db.Payments.FirstOrDefault(p => p.Id == model.Id && !p.IsDeleted);

            if (payment == null)
            {
                return(BadRequest("This payment doesn't exist or it has been deleted."));
            }

            try
            {
                payment.Amount = model.Amount;
                _db.SaveChanges();
            }
            catch (Exception)
            {
                return(BadRequest("An error occurred while trying to edit payment, please try again later"));
            }

            return(Ok());
        }
        public IHttpActionResult Add([FromBody] WishAddViewModel model)
        {
            var authToken   = Request.Headers.GetValues("authToken").FirstOrDefault()?.ToString() ?? null;
            var authTokenDb = _db.AuthenticationTokens.FirstOrDefault(at => at.Token.Equals(authToken));

            if (!authTokenDb?.IsTokenValid(authToken) ?? true)
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest("An error occurred while trying to add new wish, please try again later."));
            }

            Wishes wish = model;

            try
            {
                _db.Wishes.Add(wish);
                _db.SaveChanges();
            }
            catch (Exception)
            {
                return(BadRequest("An error occurred while trying to add new wish, please try again later."));
            }

            return(Ok());
        }
示例#4
0
        public async Task <HttpResponseMessage> Add()
        {
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            string root = HttpContext.Current.Server.MapPath("~/App_Data");

            if (!Directory.Exists(root))
            {
                Directory.CreateDirectory(root);
            }

            var provider = new MultipartFormDataStreamProvider(root);

            try
            {
                // Read the form data
                await Request.Content.ReadAsMultipartAsync(provider);

                // Get uploaded image
                Guid?guid = null;
                if (provider.FileData.Count > 0)
                {
                    guid = Guid.NewGuid();
                    MultipartFileData file  = provider.FileData[0];
                    FileStream        fs    = new FileStream(file.LocalFileName, FileMode.Open);
                    BinaryReader      br    = new BinaryReader(fs);
                    byte[]            bytes = br.ReadBytes((Int32)fs.Length);

                    // Get Image from bytes
                    Image image = null;
                    using (MemoryStream stream = new MemoryStream(bytes))
                    {
                        image = Image.FromStream(stream);
                    }

                    // Resize the image and compress
                    int width, height;
                    this.GetImageSize(image, 500, out width, out height);
                    Bitmap resized    = this.ResizeImage(image, width, height);
                    Stream compressed = this.VaryQualityLevel(resized, 80L);

                    // Save blob to azure blob storage
                    await blobProvider.SaveBlobDataAsync(WishListController.blobContainer, guid.ToString(), compressed);

                    // Add to cache
                    using (MemoryStream ms = new MemoryStream())
                    {
                        compressed.Position = 0;
                        compressed.CopyTo(ms);
                        WishListController.imageCache.Add(guid.ToString(), Convert.ToBase64String(ms.ToArray()));
                    }

                    fs.Close();
                    br.Close();
                }

                WishItem wishItem = new WishItem
                {
                    name     = provider.FormData["name"],
                    type     = provider.FormData["type"],
                    brand    = provider.FormData["brand"],
                    no       = provider.FormData["no"],
                    comment  = provider.FormData["comment"],
                    price    = provider.FormData["price"] == null ? null : (int?)int.Parse(provider.FormData["price"]),
                    currency = provider.FormData["currency"],
                    status   = 0,
                    imageId  = guid.HasValue ? guid.ToString() : null
                };

                db.WishItem.Add(wishItem);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Moved);
                string fullyQualifiedUrl     = Request.RequestUri.GetLeftPart(UriPartial.Authority);
                response.Headers.Location = new Uri(fullyQualifiedUrl + "/#/makewish?afterAdd=1");

                return(response);
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }