Esempio n. 1
0
        public HttpResponseMessage PutUser(long id, UserDto userDto)
        {
            //Check valid id
            if (!userDto.UserId.Equals(id))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, INVALID_PARAMEATER));
            }
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            using (var db = new MishWishEntities())
            {
                // Get original user detail.
                var fullUser = db.Users.FirstOrDefault(u => u.UserId == id && !u.IsDeleted);

                if (fullUser == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, USER_NOT_FOUND));
                }

                // Convert user DTO to entity.
                var userEntity = userDto.ToEntity();

                userEntity.CreatedDate = fullUser.CreatedDate;
                userEntity.UpdatedDate = DateTime.UtcNow;

                try
                {
                    // Update selected user
                    db.Entry(fullUser).CurrentValues.SetValues(userEntity);
                    db.SaveChanges();
                }
                catch (DbUpdateException ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Make sure that you have included all required fields in your request."));
                }
                catch (Exception exGeneral)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exGeneral.GetType().ToString()));
                }

                // Bind return user DTO.
                UserDto returnUser = new UserDto(userEntity)
                {
                    Message = USER_SUCCESS_UPDATED
                };

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, returnUser);
                return(response);
            }
        }
Esempio n. 2
0
        public HttpResponseMessage PutUser(long id, ContactDto contactDto)
        {
            // Check valid parameter
            if (!contactDto.ContactId.Equals(id))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, INVALID_PARAMEATER));
            }

            using (var db = new MishWishEntities())
            {
                // Get original contact detail.
                var contactUser = db.Contacts.FirstOrDefault(u => u.ContactId == id);

                // Check contact is exist or not
                if (contactUser == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, USER_NOT_FOUND));
                }

                // Convert user DTO to entity.
                var contactEntity = contactDto.ToEntity();

                try
                {
                    db.Entry(contactUser).CurrentValues.SetValues(contactEntity);
                    db.SaveChanges();
                }
                catch (DbUpdateException ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, REQUIRED_FIELD));
                }
                catch (Exception exGeneral)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exGeneral.GetType().ToString()));
                }

                // Bind return Contact DTO.
                ContactDto returnUser = new ContactDto(contactEntity)
                {
                    Message = USER_SUCCESS_UPDATED
                };

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, returnUser);
                return(response);
            }
        }
Esempio n. 3
0
        public HttpResponseMessage DeleteUser(long id)
        {
            if (id == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, INVALID_PARAMEATER));
            }

            using (var db = new MishWishEntities())
            {
                // Get original user detail.
                var fullUser = db.Users.FirstOrDefault(u => u.UserId == id && !u.IsDeleted);

                if (fullUser == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, USER_NOT_FOUND));
                }

                // Convert user DTO to entity.
                fullUser.UpdatedDate = DateTime.UtcNow;
                fullUser.IsDeleted   = true;

                try
                {
                    db.Entry(fullUser).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (DbUpdateException ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Make sure that you have included all required fields in your request."));
                }
                catch (Exception exGeneral)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exGeneral.GetType().ToString()));
                }

                // Bind return user DTO.
                UserDto returnUser = new UserDto(fullUser)
                {
                    Message = USER_SUCCESS_UPDATED
                };

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, returnUser);
                return(response);
            }
        }
Esempio n. 4
0
        public HttpResponseMessage DeleteContact(long id)
        {
            if (id == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, INVALID_PARAMEATER));
            }

            using (var db = new MishWishEntities())
            {
                // Get original contact detail.
                var contact = db.Contacts.FirstOrDefault(c => c.ContactId == id);

                if (contact == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, USER_NOT_FOUND));
                }

                // Convert contact DTO to entity.
                try
                {
                    db.Entry(contact).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (DbUpdateException ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, REQUIRED_FIELD));
                }
                catch (Exception exGeneral)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exGeneral.GetType().ToString()));
                }

                // Bind return contact DTO.
                ContactDto returnUser = new ContactDto(contact)
                {
                    Message = USER_SUCCESS_UPDATED
                };

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, returnUser);
                return(response);
            }
        }