Exemple #1
0
 public ActionResult <PetModel> CreatePet([FromBody] PetModel petModel)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return(BadRequest(petModel));
         }
         var url        = HttpContext.Request.Host;
         var createdPet = _petService.CreatePet(petModel);
         return(CreatedAtRoute("GetPet", new { petId = createdPet.Id }, createdPet));
     }
     catch (Exception ex)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, $"Something happend: {ex.Message}"));
     }
 }
        public HttpResponseMessage POST([FromBody] PetExpandedModel model)
        {
            try
            {
                // Upload Image
                var imageBinaryInBase64 = model.thumbNailInBase64;
                var avatarBinary        = Convert.FromBase64String(imageBinaryInBase64);
                var stream = new MemoryStream();
                stream.Write(avatarBinary, 0, avatarBinary.Length);
                var image    = Image.FromStream(stream);
                var filePath = HostingEnvironment.MapPath("~/Images/PetThumbnails");
                var fileName = Utils.NewGuid() + ".jpg";
                if (filePath != null)
                {
                    var fullPath = Path.Combine(filePath, fileName);
                    image.Save(fullPath);
                }

                var imageEntity = new ImageEntity()
                {
                    thumbnail = fileName
                };
                imageEntity = _iImageServices.CreateImage(imageEntity);

                // Create Pet
                model.imageId = imageEntity.id;
                var pet = Mapper.Map <PetExpandedModel, PetEntity>(model);
                pet = _iPetServices.CreatePet(pet);

                return(Request.CreateResponse(HttpStatusCode.OK, pet));
            }
            catch (Exception e)
            {
                return(ResponseHelper.ErrorResult(Request, HttpStatusCode.InternalServerError, ErrorMessages.ERROR_MSG_SERVER_ERROR, ErrorCodeStrings.SERVER_ERROR, e.ToString()));
            }
        }