Esempio n. 1
0
        public IHttpActionResult PostDestination(LifeInCity Life1)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CTEmployee empLogged = GetLoggedEmp(out bool isAdmin);

            //ako ne postoji user koji je logovan
            if (empLogged == null)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User invalid")));
            }

            //ako mije admin
            if (!isAdmin)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User not permited")));
            }

            //da li postoji destincija u listi destinacija
            bool DestExists = db.Destinations.Any(e => e.ID.Equals(Life1.IDDestination));

            if (!DestExists)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Destination not valid!")));
            }

            //da li postoji kategorija u listi kategorija
            bool CatExists = db.AccTraDesCategories.Any(e => e.ID.Equals(Life1.IDAccTraDesCategory));

            if (!CatExists)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Category not valid!")));
            }

            //da li postoji već opis te kategorije za taj grad
            bool DestAndCategExists = db.LifeInCities.Any(x => (x.IDAccTraDesCategory.Equals(Life1.IDAccTraDesCategory) && x.IDDestination.Equals(Life1.IDDestination)));

            //ako već ima, nemoj dodati
            if (DestAndCategExists)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, "Category alredy exists for given destination")));
            }

            //inače dodaj
            else
            {
                Life1.ChangeDate = System.DateTime.UtcNow;
                Life1.IDAdmin    = empLogged.ID;

                db.LifeInCities.Add(Life1);
                db.SaveChanges();

                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Created, "Category added")));
            }
        }
Esempio n. 2
0
        public IHttpActionResult DeleteLifeInCity([FromUri] int id, [FromUri] int id2 = -1)
        {
            CTEmployee empLogged = GetLoggedEmp(out bool isAdmin);

            if (empLogged == null)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User invalid")));
            }
            Country country = db.Countries.Find(id);

            if (!isAdmin)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User not permited")));
            }

            //da li postoji destincija u listi destinacija
            bool DestExists = db.Destinations.Any(e => e.ID.Equals(id));

            if (!DestExists)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Destination not valid!")));
            }

            //da li postoji kategorija u listi kategorija
            bool CatExists = db.AccTraDesCategories.Any(e => e.ID.Equals(id2));

            if (!CatExists)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Category not valid!")));
            }

            //da li postoji LifeInCity te kategorije za taj grad
            LifeInCity life1 = db.LifeInCities.Where(x => (x.IDAccTraDesCategory.Equals(id2) && x.IDDestination.Equals(id))).FirstOrDefault();

            //ako nema, nema ni brisanja
            if (life1 == null)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Category dont exist for given destination")));
            }

            //inače briši
            else
            {
                db.LifeInCities.Remove(life1);
                db.SaveChanges();

                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Moved, "Item Deleted")));
            }
        }
Esempio n. 3
0
        public IHttpActionResult PutLifeInCity(LifeInCity life1)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CTEmployee empLogged = GetLoggedEmp(out bool isAdmin);

            //ako ne postoji user koji je logovan
            if (empLogged == null)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User invalid")));
            }

            //ako mije admin
            if (!isAdmin)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User not permited")));
            }

            //postoji li LifeInCity prema iD-ju ?
            bool LifeincityExists = db.LifeInCities.Any(x => x.ID.Equals(life1.ID));

            //ako nema
            if (!LifeincityExists)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "LifeinCIty Item not found")));
            }

            life1.ChangeDate = System.DateTime.UtcNow;
            life1.IDAdmin    = empLogged.ID;

            //polja IDDestination i IDAccTraDesCategory se ne menjaju
            db.LifeInCities.Attach(life1);
            db.Entry(life1).Property("Description").IsModified = true;
            db.SaveChanges();

            return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, "Item changed")));
        }