public IHttpActionResult Put(Report value) { Report report; var response = Request.CreateResponse<Report>(HttpStatusCode.Created, value); using (var reportsService = new ReportsService()) { report = reportsService.GetById(value._id); if (report != null) { report = Mapper.Map(value, report); reportsService.Update(report); return Ok(report); } else { return BadRequest("Report could not be found"); } } }
public async Task<HttpResponseMessage> Post(string type, string id) { string imageId = null; string temPath = null; //Checks if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); //Checks if the type is valid PicType picType; if (!Enum.TryParse<PicType>(type, out picType)) throw new HttpResponseException(HttpStatusCode.InternalServerError); string path = HttpContext.Current.Server.MapPath("~/data/pics"); string tempPath = path + "\\temp"; try { using (var picturesService = new PicturesService()) { picturesService.EnsureDirectory(path + "\\temp"); var provider = new MultipartFormDataStreamProvider(tempPath); MultipartFileData fileData = null; // Read the form data and save in the path. await Request.Content.ReadAsMultipartAsync(provider); if (provider.FileData.Count <= 0) { throw new HttpResponseException(HttpStatusCode.NoContent); } fileData = provider.FileData.First(); if (!fileData.Headers.ContentType.Equals(MediaTypeHeaderValue.Parse("image/jpeg")) && !fileData.Headers.ContentType.Equals(MediaTypeHeaderValue.Parse("image/png")) && !fileData.Headers.ContentType.Equals(MediaTypeHeaderValue.Parse("image/gif"))) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } //var fileContent = provider.GetStream(provider.Contents.First(), provider.Contents.First().Headers); temPath = fileData.LocalFileName; imageId = picturesService.Save(picType, id, fileData.LocalFileName, path); //Adds the image to the corresponding document switch (picType) { //TODO:Implementar el codigo para los demás tipos case PicType.reports: using (var reportsService = new ReportsService()) { var report = reportsService.GetById(id); if(string.IsNullOrEmpty(report.picture)){ report.picture = imageId; }else{ report.detail.pics.Add(imageId); } reportsService.Update(report); }; break; case PicType.users: break; } } } catch (System.Exception e) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } finally { //deletes the temp file if (temPath != null && File.Exists(temPath)) { using (var picturesService = new PicturesService()) { picturesService.DeleteFile(temPath); } } } if (!string.IsNullOrEmpty(imageId)) { HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); imageId = Newtonsoft.Json.JsonConvert.SerializeObject(new { id = imageId }); result.Content = new ObjectContent(imageId.GetType(), imageId, GlobalConfiguration.Configuration.Formatters.JsonFormatter); return result; //return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }