Example #1
0
        public HttpResponseMessage PutSubject(int id, [FromBody] PutSubjectDTO updated)
        {
            string userId = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;

            logger.Info("UserId: " + userId + ": Requesting Update for Subject Id: " + id);

            if (updated.Id != id)
            {
                logger.Error("Updated Subject id " + updated.Id + " doesn't match the id " + id + " from the request (route).");
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Updated Subject id " + updated.Id + " doesn't match the id " + id + " from the request (route)."));
            }

            try
            {
                Subject saved = subjectsService.Update(id, updated);

                if (saved == null)
                {
                    logger.Info("Failed!");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Failed! Something went wrong."));
                }

                logger.Info("Success!");
                return(Request.CreateResponse(HttpStatusCode.OK, saved));
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e));
            }
        }
Example #2
0
        public Subject Update(int id, PutSubjectDTO updated)
        {
            Subject found = GetByID(id);

            if (found == null)
            {
                throw new HttpException("The subject with id: " + updated.Id + " was not found.");
            }

            if (updated.Name != null)
            {
                found.Name = updated.Name;
            }
            if (updated.Grade != null)
            {
                found.Grade = (int)updated.Grade;
            }
            if (updated.NumberOfClassesPerWeek != null)
            {
                found.NumberOfClassesPerWeek = (int)updated.NumberOfClassesPerWeek;
            }

            db.SubjectsRepository.Update(found);

            Subject duplicate = db.SubjectsRepository.Duplicate(found.Name, found.Grade, found.NumberOfClassesPerWeek);

            if (duplicate != null && duplicate.Id != found.Id)
            {
                throw new HttpException("The subject you are creating by this update is already in the system. " +
                                        "The subject id:" + duplicate.Id);
            }

            db.Save();
            return(found);
        }