예제 #1
0
        public async Task <bool> UploadFile(string imageFilePath, PatronDto patronDto)
        {
            try
            {
                HttpClient client = new HttpClient();

                var fileStream = File.OpenRead(imageFilePath);

                var content       = new MultipartFormDataContent();
                var streamContent = new StreamContent(fileStream);
                var requestUri    = PaintingGalleryConstants.PaintingGalleryApi + "api/uploads/files/image/patron/" + patronDto.Id;

                streamContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
                content.Add(streamContent, "image", imageFilePath);

                var reponse = await client.PostAsync(requestUri, content);

                fileStream.Close();

                if (reponse.IsSuccessStatusCode)
                {
                    return(true);
                }

                return(false);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public IHttpActionResult PutPatron([FromBody] PatronDto patronDto)
        {
            try
            {
                if (patronDto == null)
                {
                    return(BadRequest());
                }

                var result = _manager.EditPatron(_mapper.Map <Patron>(patronDto));

                if (result.Status == RepositoryActionStatus.Updated)
                {
                    return(Ok(_mapper.Map <PatronDto>(result.Entity)));
                }

                if (result.Status == RepositoryActionStatus.NotFound)
                {
                    return(NotFound());
                }

                return(BadRequest());
            }
            catch (System.Exception)
            {
                return(InternalServerError());
            }
        }
예제 #3
0
        public async Task <PatronDto> EditPatron(PatronDto patronDto)
        {
            try
            {
                var       client = PaintingGalleryHttpClient.GetClient();
                var       serealizedItemToCreate = JsonConvert.SerializeObject(patronDto);
                PatronDto patronEdited           = null;

                var response = await client.PutAsync("api/patrons",
                                                     new StringContent(serealizedItemToCreate, System.Text.Encoding.Unicode, "application/json"));

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();

                    patronEdited = JsonConvert.DeserializeObject <PatronDto>(content);
                }

                return(patronEdited);
            }
            catch (System.Exception)
            {
                throw;
            }
        }
        public IHttpActionResult PostPatron([FromBody] PatronDto patronDto)
        {
            try
            {
                if (patronDto == null)
                {
                    return(BadRequest());
                }

                var result = _manager.RegisterPatron(_mapper.Map <Patron>(patronDto));

                if (result.Status == RepositoryActionStatus.Created)
                {
                    var imageUrl = string.Format("{0}/{1}/userImage?filename={2}", Request.RequestUri, result.Entity.Id, result.Entity.Picture);

                    result.Entity.Picture = imageUrl;

                    return(Created(Request.RequestUri + "/" + result.Entity.Id.ToString(), _mapper.Map <PatronDto>(result.Entity)));
                }

                if (result.Status == RepositoryActionStatus.Error)
                {
                    return(InternalServerError());
                }

                return(BadRequest());
            }
            catch (System.Exception)
            {
                return(InternalServerError());
            }
        }
예제 #5
0
        public async Task <bool> Add(PatronDto newPatronDto)
        {
            var newPatron = _mapper.Map <Patron>(newPatronDto);
            await _context.AddAsync(newPatron);

            await _context.SaveChangesAsync();

            return(true);
        }
 private void FillInputs(PatronDto patron)
 {
     txtIdentification.Text = patron.Identification;
     txtName.Text           = patron.Name;
     txtCountry.Text        = patron.Country;
     txtCity.Text           = patron.City;
     txtBirthdate.Text      = patron.Birthdate.ToString();
     txtDeath.Text          = patron.Death.ToString();
     imageContainer.Source  = new BitmapImage(new Uri(patron.Picture));
 }
예제 #7
0
 private static void ResolveMapper(out PatronDto patronDto, out IMapper mapper, out Patron patron)
 {
     patronDto = PatronDtoBuilder.BuildRandom();
     patron    = new Patron
     {
         PatronId = patronDto.PatrolDtoId
     };
     mapper = Substitute.For <IMapper>();
     mapper.Map <Patron>(patronDto).Returns(patron);
 }
예제 #8
0
        public async Task <ServiceResult <Guid> > Add(PatronDto patronDto)
        {
            var newPatron = _mapper.Map <Patron>(patronDto);
            await _context.Patrons.AddAsync(newPatron);

            await _context.SaveChangesAsync();

            return(new ServiceResult <Guid>
            {
                Data = newPatron.PatronId,
                Error = null
            });
        }
        private async void ExecuteAction(string action)
        {
            try
            {
                PatronDto      patron  = null;
                MessageControl message = null;

                if (!action.Equals("Save"))
                {
                    patron  = await new Services.PatronServices().CreatePatron(GetPatronInfo());
                    message = MessageControl.SuccessfulRegistration();
                }
                else
                {
                    patron  = await new Services.PatronServices().EditPatron(GetPatronInfo());
                    message = MessageControl.SuccessfulChanges();
                }

                if (patron != null)
                {
                    if (imageContainer.Tag != null && imageContainer.Tag.ToString().Equals("hasChanged"))
                    {
                        var result = await Services.FileServices.GetInstance().UploadFile(OpenFileDialog.FileName, patron);

                        if (result == false)
                        {
                            new MessageControl("Error", "We had an error while trying to upload the image, please try again in a few minutes", MessageType.Error)
                            .ShowMessage();
                        }
                        else
                        {
                            message.ShowMessage();
                            MainWindow.DisplayScreen(new ShowPatrons());
                        }
                    }
                    else
                    {
                        message.ShowMessage();
                        MainWindow.DisplayScreen(new ShowPatrons());
                    }
                }
                else
                {
                    MessageControl.RegistrationFailed().ShowMessage();
                }
            }
            catch (Exception)
            {
                MessageControl.Error().ShowMessage();
            }
        }
예제 #10
0
        public IHttpActionResult CreatePatron(PatronDto patronDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var patron = Mapper.Map <PatronDto, Patron>(patronDto);

            _context.Patrons.Add(patron);
            _context.SaveChanges();

            patronDto.Id = patron.Id;

            return(Created(new Uri(Request.RequestUri + "/" + patron.Id), patronDto));
        }
예제 #11
0
        public void UpdatePatron(int id, PatronDto patronDto)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var patronInDb = _context.Patrons.SingleOrDefault(c => c.Id == id);

            if (patronDto == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            Mapper.Map <PatronDto, Patron>(patronDto, patronInDb);


            _context.SaveChanges();
        }
예제 #12
0
        public async Task <PatronDto> GetPatron(int id)
        {
            try
            {
                var client = PaintingGalleryHttpClient.GetClient();
                var result = await client.GetAsync("api/patrons?id=" + id);

                PatronDto patron = null;

                if (result.IsSuccessStatusCode)
                {
                    var content = await result.Content.ReadAsStringAsync();

                    patron = JsonConvert.DeserializeObject <PatronDto>(content);
                }

                return(patron);
            }
            catch (System.Exception)
            {
                throw;
            }
        }