Exemple #1
0
        public async Task <HttpResponseMessage> Remove(int id)
        {
            HttpResponseMessage response;

            try
            {
                var rental = _rentalBusiness.Get(id);
                if (rental == null)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = Consts.C_RENTAL_NOT_FOUND;
                }
                else
                {
                    _rentalBusiness.Delete(id);
                    await _rentalBusiness.ApplyChagesAsync();

                    response         = Request.CreateResponse();
                    response.Content = new StringContent(Consts.C_RENTAL_DELETED);
                }
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }
Exemple #2
0
        public HttpResponseMessage Get(int id)
        {
            HttpResponseMessage response;

            try
            {
                var rental = _rentalBusiness.Get(id);
                if (rental == null)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = Consts.C_RENTAL_NOT_FOUND;
                }
                else
                {
                    response         = Request.CreateResponse();
                    response.Content = new ObjectContent <Rental>(rental, fJsonMTF, Consts.C_MT_JSON);
                }
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }
Exemple #3
0
        public async Task <HttpResponseMessage> AddAsync(User user)
        {
            HttpResponseMessage response;

            try
            {
                if (_userBusiness.ValidateUser(user))
                {
                    _userBusiness.Add(user);
                    await _userBusiness.ApplyChagesAsync();

                    response         = Request.CreateResponse();
                    response.Content = new ObjectContent <User>(user, fJsonMTF, Consts.C_MT_JSON);
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.BadRequest);
                    response.ReasonPhrase = Consts.VALIDATION_ERROR_RESPONSE_PHRASE;
                    response.Content      = new StringContent(Consts.INVALID_USER_DATA);
                }
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }
Exemple #4
0
        public HttpResponseMessage Get()
        {
            HttpResponseMessage response;

            try
            {
                var users = _userBusiness.List();
                if (!users.Any())
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = Consts.C_USER_NOT_FOUND;
                }
                else
                {
                    var usersInfoList = _userBusiness.List().Select(u => new
                    {
                        u.Id,
                        u.Login
                    });
                    response         = Request.CreateResponse();
                    response.Content = new ObjectContent <IEnumerable <dynamic> >(usersInfoList, fJsonMTF, Consts.C_MT_JSON);
                }
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }
        public HttpResponseMessage Get()
        {
            HttpResponseMessage response;

            try
            {
                var genders = _genderBusiness.List();
                if (!genders.Any())
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = Consts.C_GENDER_NOT_FOUND;
                }
                else
                {
                    response         = Request.CreateResponse();
                    response.Content = new ObjectContent <IEnumerable <Gender> >(genders, fJsonMTF, Consts.C_MT_JSON);
                }
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }
        public async Task <HttpResponseMessage> Update(int id, Gender gender)
        {
            HttpResponseMessage response;

            try
            {
                var repoGender = _genderBusiness.Get(id);
                if (repoGender == null)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = Consts.C_GENDER_NOT_FOUND;
                }
                else
                {
                    _genderBusiness.Update(id, gender);
                    await _genderBusiness.ApplyChagesAsync();

                    response         = Request.CreateResponse();
                    response.Content = new ObjectContent <Gender>(repoGender, fJsonMTF, Consts.C_MT_JSON);
                }
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }
Exemple #7
0
        public async Task <HttpResponseMessage> Add(Movie movie)
        {
            HttpResponseMessage response;

            try
            {
                if (_movieBusiness.ValidateMovieRelations(movie))
                {
                    _movieBusiness.FillGender(movie, movie.Gender.Id);
                    _movieBusiness.Add(movie);
                    await _movieBusiness.ApplyChagesAsync();

                    response         = Request.CreateResponse();
                    response.Content = new ObjectContent <Movie>(movie, fJsonMTF, Consts.C_MT_JSON);
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = Consts.C_GENDER_NOT_FOUND;
                    response.Content      = new StringContent($"Gender with id {movie.Gender.Id} does not existis. Crate this Gender first");
                }
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }
Exemple #8
0
        public async Task <HttpResponseMessage> Update(int id, Rental rental)
        {
            HttpResponseMessage response = null;

            try
            {
                var repoRental = _rentalBusiness.Get(id);
                if (repoRental == null)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = Consts.C_MOVIE_NOT_FOUND;
                }
                else if (!CPFUtils.ValidateFormat(rental.CustomerCPF))
                {
                    response = Request.CreateResponse(HttpStatusCode.BadRequest);
                    response.ReasonPhrase = Consts.VALIDATION_ERROR_RESPONSE_PHRASE;
                    response.Content      = new StringContent(Consts.C_RENTAL_CPF_ERROR_MESSAGE);
                }
                else if (!_rentalBusiness.ValidateRentalRelations(rental))
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = Consts.C_MOVIE_NOT_FOUND;
                    response.Content      = new StringContent(Consts.C_INVALID_RENTAL_ERROR_MESSAGE);
                }
                else
                {
                    IEnumerable <int> moviesIds = rental.MoviesList.Select(m => m.Id);
                    _rentalBusiness.FillRentalMovies(rental, moviesIds);

                    _rentalBusiness.Update(id, rental);
                    await _rentalBusiness.ApplyChagesAsync();

                    response         = Request.CreateResponse();
                    response.Content = new ObjectContent <Rental>(rental, fJsonMTF, Consts.C_MT_JSON);
                }
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }
        public async Task <HttpResponseMessage> AddAsync(Gender gender)
        {
            HttpResponseMessage response;

            try
            {
                _genderBusiness.Add(gender);
                await _genderBusiness.ApplyChagesAsync();

                response         = Request.CreateResponse();
                response.Content = new ObjectContent <Gender>(gender, fJsonMTF, Consts.C_MT_JSON);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ExceptionUtils.GetErrorMessages(ex);
            }
            return(response);
        }