Esempio n. 1
0
        // POST api/EmployeesExpenses
        public HttpResponseMessage PostEmployeesExpenses(EmployeesExpenses employeesexpenses)
        {
            if (ModelState.IsValid)
            {
                string CustomCode = "EE-" + DateTime.Now.ToString("yyyyMMdd");
                int? MaxCode = Convert.ToInt32((db.EmployeesExpenses.Where(r => r.EmployeesExpensesCode.StartsWith(CustomCode)).Select(r => r.EmployeesExpensesCode.Substring(CustomCode.Length, 4)).ToList()).Max());
                string PBCode = CustomCode + ((MaxCode + 1).ToString()).PadLeft(4, '0');
                employeesexpenses.EmployeesExpensesCode = PBCode;
                employeesexpenses.SubmitDate = DateTime.Now;
                employeesexpenses.InsertBy = loginUser.UserID;
                db.EmployeesExpenses.Add(employeesexpenses);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, employeesexpenses);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = employeesexpenses.EmployeesExpensesID }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
Esempio n. 2
0
        // PUT api/EmployeesExpenses/5
        public HttpResponseMessage PutEmployeesExpenses(long id, EmployeesExpenses employeesexpenses)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != employeesexpenses.EmployeesExpensesID)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            employeesexpenses.Employee = null;

            employeesexpenses.UpdateBy = loginUser.UserID;
            db.Entry(employeesexpenses).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }