Exemple #1
0
        public IHttpActionResult PutGuard(long id, GuardContract guard)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != guard.PersonalCode)
            {
                return(BadRequest());
            }
            db.Entry(guard.ToInternal()).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GuardExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemple #2
0
        public static GuardInternal ToInternal(this GuardContract entity)
        {
            if (entity == null)
            {
                return(null);
            }
            DormitoryInternal guardDormitory = new DormitoryInternal();

            using (MainDbModelContainer1 db = new MainDbModelContainer1())
            {
                guardDormitory = db.DormitorySet.Find(entity.DormitoryId);
            }

            var guard = new GuardInternal
            {
                Name         = entity.Name,
                PersonalCode = entity.PersonalCode,
                Surname      = entity.Surname,
                Dormitory    = guardDormitory,
                Username     = entity.Username,
                Password     = entity.Password
            };

            return(guard);
        }
Exemple #3
0
        public IHttpActionResult PostGuard(GuardContract guard)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Guard temp = guard.ToInternal();

            db.DormitorySet.Attach(temp.Dormitory);
            db.GuardSet.Add(temp);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (GuardExists(guard.PersonalCode))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = guard.PersonalCode }, guard));
        }
Exemple #4
0
        public IHttpActionResult GetGuard(string user)
        {
            GuardContract guard = db.GuardSet.FirstOrDefault(x => x.Username == user).ToContract();

            if (guard == null)
            {
                return(NotFound());
            }

            return(Ok(guard));
        }
Exemple #5
0
        public IHttpActionResult GetGuard(long id)
        {
            GuardContract guard = db.GuardSet.Find(id).ToContract();

            if (guard == null)
            {
                return(NotFound());
            }

            return(Ok(guard));
        }
Exemple #6
0
        public IHttpActionResult DeleteGuard(long id)
        {
            Guard         temp  = db.GuardSet.Find(id);
            GuardContract guard = temp.ToContract();

            if (guard == null)
            {
                return(NotFound());
            }

            db.GuardSet.Remove(temp);
            db.SaveChanges();

            return(Ok(guard));
        }
Exemple #7
0
        public static GuardContract ToContract(this GuardInternal entity)
        {
            if (entity == null)
            {
                return(null);
            }

            var guard = new GuardContract
            {
                Name         = entity.Name,
                PersonalCode = entity.PersonalCode,
                Surname      = entity.Surname,
                DormitoryId  = entity.Dormitory.ID,
                Username     = entity.Username,
                Password     = entity.Password
            };

            return(guard);
        }