Example #1
0
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (var context = new LaborExchange2Entities1())
                {
                    var o = (from c in context.CompanyType where c.ID == id select c).FirstOrDefault();
                    if (o == null)
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound));
                    }
                    context.CompanyType.Remove(o);
                    context.SaveChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
            }
            catch (DbUpdateException dbex)
            {
                return(Request.CreateResponse(HttpStatusCode.Conflict,
                                              dbex.ToString()

                                              ));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError,
                                              ex.ToString()
                                              ));
            }
        }
Example #2
0
        public PagedResult <CompanyTypeModel> GetPagedCTs(int page, int pagecount)
        {
            using (var c = new LaborExchange2Entities1())
            {
                var rr  = new PagedResult <CompanyTypeModel>();
                var tt  = (from o in c.CompanyType select o).OrderBy(o => o.ID);
                int cc  = tt.Count();
                var ttx = tt.Skip((page - 1) * pagecount).Take(pagecount).ToArray();

                List <CompanyTypeModel> res = new List <CompanyTypeModel>();
                foreach (var item in ttx)
                {
                    res.Add(new CompanyTypeModel {
                        ID = item.ID, Company = item.Type
                    });
                }
                rr.Page = res.ToArray();
                var tc = tt.Count();
                if (tc % pagecount == 0)
                {
                    rr.PageCount = tc / pagecount;
                }
                else
                {
                    rr.PageCount = tc / pagecount + 1;
                }
                return(rr);
            }
        }
Example #3
0
        public HttpResponseMessage Put(int id, [FromBody] MyModel mdl)
        {
            if (id == -1 || mdl?.NewName == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            try
            {
                using (var context = new LaborExchange2Entities1())
                {
                    var o = (from c in context.CompanyType where c.ID == id select c).FirstOrDefault();
                    if (o == null)
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound));
                    }
                    o.Type = mdl.NewName;
                    context.SaveChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
            }
            catch (DbUpdateException dbex)
            {
                return(Request.CreateResponse(HttpStatusCode.Conflict,
                                              dbex.ToString()

                                              ));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError,
                                              ex.ToString()
                                              ));
            }
        }
Example #4
0
        public PagedResult <CompanyTypeModel> GetCTs(int id = -1)
        {
            using (var c = new LaborExchange2Entities1())
            {
                var rr = new PagedResult <CompanyTypeModel>();
                var tt = c.CompanyType;
                int cc = tt.Count();

                List <CompanyTypeModel> res;
                res = id == -1 ?
                      tt.Select(companyType => new CompanyTypeModel
                {
                    ID      = companyType.ID,
                    Company = companyType.Type
                }).ToList() :
                      (from companyType in tt
                       where companyType.ID == id
                       select new CompanyTypeModel
                {
                    ID = companyType.ID,
                    Company = companyType.Type
                }).ToList();
                rr.Page      = res.ToArray();
                rr.PageCount = 1;
                return(rr);
            }
        }
Example #5
0
        public string Post([FromBody] MyModel mdl)
        {
            using (var context = new LaborExchange2Entities1())
            {
                if (mdl == null || mdl.NewName == "")
                {
                    return(null);
                }

                if (Enumerable.Any(context.CompanyType, companyType => companyType.Type ==
                                   mdl.NewName))
                {
                    return("Already exists");
                }

                CompanyType ct = new CompanyType
                {
                    Type = mdl.NewName
                };
                context.CompanyType.Add(ct);
                try
                {
                    context.SaveChanges();
                    return("OK");
                }
                catch (DbException dbex)
                {
                    return
                        (dbex.ToString());
                }
                catch (Exception exception)
                {
                    return(exception.Message);
                }
            }
        }