示例#1
0
        public async Task <IActionResult> PutMemeItemModel([FromRoute] int id, [FromBody] MemeItemModel memeItemModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != memeItemModel.Id)
            {
                return(BadRequest());
            }

            _context.Entry(memeItemModel).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MemeItemModelExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
示例#2
0
        public async Task <IActionResult> PostMemeItemModel([FromBody] MemeItemModel memeItemModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.MemeItemModel.Add(memeItemModel);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetMemeItemModel", new { id = memeItemModel.Id }, memeItemModel));
        }
示例#3
0
        public async Task <IActionResult> UploadFile([FromForm] MemeImageItemModel meme)
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }
            try
            {
                using (var stream = meme.Image.OpenReadStream())
                {
                    var cloudBlock = await UploadToBlob(meme.Image.FileName, null, stream);

                    //// Retrieve the filename of the file you have uploaded
                    //var filename = provider.FileData.FirstOrDefault()?.LocalFileName;
                    if (string.IsNullOrEmpty(cloudBlock.StorageUri.ToString()))
                    {
                        return(BadRequest("An error has occured while uploading your file. Please try again."));
                    }

                    MemeItemModel memeItem = new MemeItemModel();
                    memeItem.Title = meme.Title;
                    memeItem.Tags  = meme.Tags;

                    System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
                    memeItem.Height   = image.Height.ToString();
                    memeItem.Width    = image.Width.ToString();
                    memeItem.Url      = cloudBlock.SnapshotQualifiedUri.AbsoluteUri;
                    memeItem.Uploaded = DateTime.Now.ToString();

                    _context.MemeItemModel.Add(memeItem);
                    await _context.SaveChangesAsync();

                    return(Ok($"File: {meme.Title} has successfully uploaded"));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest($"An error has occured. Details: {ex.Message}"));
            }
        }