Exemple #1
0
        public async Task <HttpResponseMessage> Companies(CompaniesDataModel model)
        {
            Thread.CurrentThread.CurrentCulture = culture;

            List <CompaniesQueryModel> resultSeachModel     = null;
            CompaniesByIdModel         resultSeachByIdModel = null;
            Companies proxy = new Companies();

            try
            {
                if (!await Authentication.isAdmin(User, Request))
                {
                    Authentication auth = new Authentication();

                    if (!await auth.AccesRights(User.Identity.GetUserId(), "companies", model.type))
                    {
                        return(Request.CreateResponse(System.Net.HttpStatusCode.Unauthorized));
                    }
                    auth = null;
                }

                using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    using (SqlConnection connection1 = new SqlConnection(connetionString))
                    {
                        await connection1.OpenAsync();

                        if (model.type == 1)
                        {
                            resultSeachModel = new List <CompaniesQueryModel>();
                            await proxy.SearchQuery(model.search, resultSeachModel, connection1);
                        }
                        else if (model.type == 2)
                        {
                            resultSeachByIdModel = new CompaniesByIdModel();
                            await proxy.ById(model.byId, connection1, resultSeachByIdModel);
                        }
                        else if (model.type == 3)
                        {
                            await proxy.New(connection1, model.update);
                        }
                        else if (model.type == 4)
                        {
                            await proxy.Update(connection1, model.update);
                        }
                        else if (model.type == 5)
                        {
                            await proxy.UpdateIsActive(connection1, model.isActive);
                        }
                    }
                    scope.Complete();
                }
            }
            catch (TransactionAbortedException ex)
            {
                ErrorModel _errors = new ErrorModel();
                _errors.message = ex.Message;
                return(Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, _errors));
            }
            catch (Exception ex)
            {
                ErrorModel _errors = new ErrorModel();
                _errors.message = ex.Message;
                return(Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, _errors));
            }

            if (model.type == 1)
            {
                return(Request.CreateResponse(System.Net.HttpStatusCode.OK, resultSeachModel));
            }
            else if (model.type == 2)
            {
                return(Request.CreateResponse(System.Net.HttpStatusCode.OK, resultSeachByIdModel));
            }

            return(Request.CreateResponse(System.Net.HttpStatusCode.OK));
        }