Example #1
0
        public async Task <Response> Insert(Company company)
        {
            Response response = Validate(company);

            if (response.Success)
            {
                company.CPNJ   = company.CPNJ.RemoveMask();
                company.Active = true;
                try
                {
                    using (KnowledgeTestDB dataBase = new KnowledgeTestDB())
                    {
                        dataBase.Companies.Add(company);
                        await dataBase.SaveChangesAsync();

                        return(ResponseMessage.CreateSuccessResponse());
                    }
                }
                catch (Exception ex)
                {
                    return(ResponseMessage.CreateErrorResponse(ex));
                }
            }
            return(ResponseMessage.CreateErrorResponse(response.Exception));
        }
Example #2
0
 public async Task <Response> Delete(int id)
 {
     try
     {
         Company company = new Company();
         company.ID = id;
         using (KnowledgeTestDB dataBase = new KnowledgeTestDB())
         {
             dataBase.Entry(company).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
             await dataBase.SaveChangesAsync();
         }
         return(ResponseMessage.CreateSuccessResponse());
     }
     catch (Exception ex)
     {
         return(ResponseMessage.CreateErrorResponse(ex));
     }
 }
Example #3
0
        public async Task <Response> Update(Company company)
        {
            Response Response = Validate(company);

            if (Response.Success)
            {
                try
                {
                    using (KnowledgeTestDB dataBase = new KnowledgeTestDB())
                    {
                        dataBase.Entry(company).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        await dataBase.SaveChangesAsync();
                    }
                    return(ResponseMessage.CreateSuccessResponse());
                }
                catch (Exception ex)
                {
                    return(ResponseMessage.CreateErrorResponse(ex));
                }
            }
            return(ResponseMessage.CreateErrorResponse(Response.Exception));
        }
Example #4
0
        public async Task <Response> Insert(Supplier supplier)
        {
            Response response = Validate(supplier);

            if (response.Success)
            {
                supplier.CNPJ_CPF         = supplier.CNPJ_CPF.RemoveMask();
                supplier.Telephone        = supplier.Telephone.RemoveMaskPhoneNumber();
                supplier.DateRegistration = DateTime.Now;
                supplier.Active           = true;
                if (!string.IsNullOrWhiteSpace(supplier.Telephone2))
                {
                    supplier.Telephone2.RemoveMaskPhoneNumber();
                }
                if (supplier.CNPJ_CPF.Length == 11)
                {
                    supplier.RG = supplier.RG.RemoveMask();
                }
                try
                {
                    using (KnowledgeTestDB dataBase = new KnowledgeTestDB())
                    {
                        dataBase.Companies.AttachRange(supplier.Companies);
                        dataBase.Suppliers.Add(supplier);
                        await dataBase.SaveChangesAsync();

                        return(ResponseMessage.CreateSuccessResponse());
                    }
                }
                catch (Exception ex)
                {
                    return(ResponseMessage.CreateErrorResponse(ex));
                }
            }
            return(ResponseMessage.CreateErrorResponse(response.Exception));
        }