public int Add(SubmittedAssignmentViewModel sub)
        {
            tblSubmittedAssignment tb = new tblSubmittedAssignment();

            tb.Routine_Id          = sub.Routine_Id;
            tb.Student_Id          = sub.Student_Id;
            tb.Submitted_Date      = System.DateTime.Now;
            tb.Checked_Status      = 0;
            tb.Feedback_Status     = 0;
            tb.Marking             = 0;
            tb.Assignmnet_Location = 0;
            _db.tblSubmittedAssignments.Add(tb);
            _db.SaveChanges();
            tblLog tl = new tblLog();

            tl.Date                  = DateTime.Now;
            tl.ActivityId            = 1;
            tl.SubmittedAssignmentId = tb.ID;
            _db.tblLogs.Add(tl);
            return(_db.SaveChanges());
        }
        public HttpResponseMessage Put([Bind(Include = "Routine_Id,Model")] SubmittedAssignmentViewModel sub)
        {
            AssignmentManagementSystemEntities _db = new AssignmentManagementSystemEntities();
            string message = "Assignment Updated Successfully";

            if (ModelState.IsValid)
            {
                bool isexists = subDB.StudentAssignmentMatch(sub.Student_Id, sub.Routine_Id);
                if (!isexists)
                {
                    ModelState.AddModelError("Routine_Id", "This Assignment is not Assigned to selected Student");
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                }
                else
                {
                    tblSubmittedAssignment tb = _db.tblSubmittedAssignments.Where(a => a.ID == sub.Id).FirstOrDefault();
                    if (tb.Assignmnet_Location == 1 && sub.Assignmnet_Location == 2)
                    {
                        ModelState.AddModelError("Routine_Id", "This Assignment can not be directly handover to student from teacher Location");
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                    }
                    else if (tb.Assignmnet_Location == 2 && sub.Assignmnet_Location == 1)
                    {
                        ModelState.AddModelError("Routine_Id", "The Assignment can not be directly handover to Teacher  from Student Location");
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                    }
                    else
                    {
                        subDB.Update(sub);
                        return(Request.CreateResponse(HttpStatusCode.OK, message));
                    }
                }
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
        public HttpResponseMessage Post([Bind(Include = "Routine_Id,Model")] SubmittedAssignmentViewModel sub)
        {
            string message = "Assignment Submitted Successfully";

            if (ModelState.IsValid)
            {
                bool isexists = subDB.StudentAssignmentMatch(sub.Student_Id, sub.Routine_Id);
                if (!isexists)
                {
                    ModelState.AddModelError("Routine_Id", "This Assignment is not Assigned to selected Student");
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                }
                else
                {
                    subDB.Add(sub);
                    return(Request.CreateResponse(HttpStatusCode.OK, message));
                }
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
        public int Update(SubmittedAssignmentViewModel sub)
        {
            ////tblSubmittedAssignment tb = _db.tblSubmittedAssignments.Where(a => a.ID == sub.Id).FirstOrDefault();
            ////tb.Routine_Id = sub.Routine_Id;
            ////tb.Student_Id = sub.Student_Id;
            ////tb.Submitted_Date = tb.Submitted_Date;
            ////tb.Checked_Status = sub.Checked_Status;
            ////tb.Feedback_Status = sub.Feedback_Status;
            ////tb.Assignmnet_Location = sub.Assignmnet_Location;
            ////tb.Marking = sub.Marking;
            ////_db.SaveChanges();
            ////tblLog tl = new tblLog();
            ////if (tb.Assignmnet_Location == 1)
            ////{
            ////    tl.Date = DateTime.Now;
            ////    tl.ActivityId = 2;
            ////    tl.SubmittedAssignmentId = tb.ID;
            ////    _db.tblLogs.Add(tl);
            ////    return _db.SaveChanges();
            ////}
            ////else if (tb.Assignmnet_Location == 0)
            ////{
            ////    tl.Date = DateTime.Now;
            ////    tl.ActivityId = 3;
            ////    tl.SubmittedAssignmentId = tb.ID;
            ////    _db.tblLogs.Add(tl);
            ////    return _db.SaveChanges();
            ////}
            ////else if (tb.Assignmnet_Location == 2)
            ////{
            ////    tl.Date = DateTime.Now;
            ////    tl.ActivityId = 4;
            ////    tl.SubmittedAssignmentId = tb.ID;
            ////    _db.tblLogs.Add(tl);
            ////    return _db.SaveChanges();
            ////}
            ////else
            ////{
            ////    tl.Date = DateTime.Now;
            ////    tl.ActivityId = 4;
            ////    tl.SubmittedAssignmentId = tb.ID;
            ////    _db.tblLogs.Add(tl);
            ////    return _db.SaveChanges();
            ////}
            tblSubmittedAssignment tb = _db.tblSubmittedAssignments.Where(a => a.ID == sub.Id).FirstOrDefault();
            tblLog tl = new tblLog();

            if (tb.Assignmnet_Location == 0 && sub.Assignmnet_Location == 1)
            {
                tl.Date                  = DateTime.Now;
                tl.ActivityId            = 2;
                tl.SubmittedAssignmentId = tb.ID;
                _db.tblLogs.Add(tl);
            }
            if (tb.Assignmnet_Location == 1 && sub.Assignmnet_Location == 0)
            {
                tl.Date                  = DateTime.Now;
                tl.ActivityId            = 3;
                tl.SubmittedAssignmentId = tb.ID;
                _db.tblLogs.Add(tl);
            }
            if (tb.Assignmnet_Location == 0 && sub.Assignmnet_Location == 2)
            {
                tl.Date                  = DateTime.Now;
                tl.ActivityId            = 4;
                tl.SubmittedAssignmentId = tb.ID;
                _db.tblLogs.Add(tl);
            }
            if (tb.Assignmnet_Location == 2 && sub.Assignmnet_Location == 0)
            {
                tl.Date                  = DateTime.Now;
                tl.ActivityId            = 5;
                tl.SubmittedAssignmentId = tb.ID;
                _db.tblLogs.Add(tl);
            }
            tb.Routine_Id          = sub.Routine_Id;
            tb.Student_Id          = sub.Student_Id;
            tb.Submitted_Date      = tb.Submitted_Date;
            tb.Checked_Status      = sub.Checked_Status;
            tb.Feedback_Status     = sub.Feedback_Status;
            tb.Marking             = sub.Marking;
            tb.Assignmnet_Location = sub.Assignmnet_Location;
            return(_db.SaveChanges());
        }