Пример #1
0
        public UserApiModel ApiUser(User user)
        {
            var apiuser = new UserApiModel
            {
                Id = user.Id,
                Email = user.Email,
                First_name = user.First_name,
                Last_name = user.Last_name
            };

            return apiuser;
        }
Пример #2
0
        public User ApiModelToDBModel(UserApiModel user)
        {
            var dbUser = new User
            {
                Id=user.Id,
                Email = user.Email,
                First_name = user.First_name,
                Last_name = user.Last_name,
                Passwrd = "123"
            };

            return dbUser;
        }
        // POST api/UserRequest
        public HttpResponseMessage PostUser(UserApiModel user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(convert.ApiModelToDBModel(user));
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, user);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.Id }));
                Logger.Log.Debug("");
                Logger.Log.Debug("Method POST was requested.Status code: 201. New user was successfuly added.");
                return response;
            }
            else
            {
                Logger.Log.Debug("Bad request occured.");
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
        // PUT api/UserRequest/5
        public HttpResponseMessage PutUser(int id, UserApiModel user)
        {
            if (ModelState.IsValid && id == user.Id)
            {
                db.Entry(convert.ApiModelToDBModel(user)).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    Logger.Log.Debug("NotFound exception occured. Status code: 404");
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }
                Logger.Log.Debug("User with id:" + id + "has been changed. Method: PUT requested. Status code: 201");
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                Logger.Log.Debug("Bad request occured.ModelState is not valid or user with id:" + id + "was not found.Status code: 404");
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }