Пример #1
0
        /// <summary>
        /// Add an attendance to the database
        /// </summary>
        /// <param name="att"></param>
        /// <returns></returns>
        public HttpResponseMessage Post([FromBody] attendance att)
        {
            try
            {
                using (ebuilderEntities entities = new ebuilderEntities())
                {
                    if (DateTime.Compare(DateTime.Today, att.date.Date) < 0)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Attendance for future dates are not allowed"));
                    }
                    if (att.EID == null || att.checkIn == null || att.checkOut == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Some fields are empty"));
                    }
                    var entity = entities.attendances.FirstOrDefault(a => a.EID == att.EID && a.date == att.date);
                    if (entity != null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Attendance the given EID and date already exists."));
                    }

                    entities.attendances.Add(att);
                    entities.SaveChanges();
                    var message = Request.CreateResponse(HttpStatusCode.OK, att);
                    message.Headers.Location = new Uri(Request.RequestUri + att.AID.ToString());
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, att.ToString(), ex));
            }
        }