Example #1
0
        public ActionResult <CaseCreateDto> UpdateCase(Guid id, CaseUpdateDto caseUpdateDto)
        {
            var caseModelFromRepo = _repository.GetCaseById(id);

            if (caseModelFromRepo != null)
            {
                //Dbcontext tracks changes mapped with the DTO class
                _mapper.Map(caseUpdateDto, caseModelFromRepo);

                if (!companyExists(caseModelFromRepo.CompanyId))
                {
                    return(NotFound("CompanyId invalid"));
                }
                ;

                _repository.UpdateCase(caseModelFromRepo);
                _repository.SaveChanges();

                return(NoContent());
            }
            return(NotFound());
        }
Example #2
0
        public async Task <HttpResponseMessage> PostFormData(int id)
        {
            try
            {
                // Check if the request contains multipart/form-data.
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

                Case theCase = _caseRepo.GetCaseById(id);

                if (theCase == null)
                {
                    throw new Exception("No such case");
                }

                //check if folder exist if not create it...
                var    pathToAttachments = "~/attachments/";
                string root = HttpContext.Current.Server.MapPath(pathToAttachments);

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

                var provider = new MultipartFormDataStreamProvider(root);


                // Read the form data. and save it
                await Request.Content.ReadAsMultipartAsync(provider);


                foreach (MultipartFileData file in provider.FileData)
                {
                    if (string.IsNullOrEmpty(file.Headers.ContentDisposition.FileName))
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
                    }

                    string fileName = file.Headers.ContentDisposition.FileName;
                    if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                    {
                        fileName = fileName.Trim('"');
                    }
                    if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                    {
                        fileName = Path.GetFileName(fileName);
                    }

                    string extension = Path.GetExtension(fileName);
                    if (extension != ".pdf")
                    {
                        throw new Exception("Not supported format");
                    }
                    fileName = id + extension;

                    exists = File.Exists(Path.Combine(root, fileName));
                    if (exists)
                    {
                        File.Delete(Path.Combine(root, fileName));
                    }

                    File.Move(file.LocalFileName, Path.Combine(root, fileName));


                    var filePath = ("attachments/" + fileName);
                    // file is uploaded


                    theCase.attachment = filePath;


                    _caseRepo.UpdateCase(theCase);



                    //return Request.CreateResponse(HttpStatusCode.OK);
                }
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
            return(Request.CreateResponse(HttpStatusCode.OK));
        }