Exemplo n.º 1
0
        public UserDto GetUserById(long id)
        {
            // Check valid id
            if (id == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, INVALID_PARAMEATER));
            }
            try
            {
                using (var db = new MishWishEntities())
                {
                    var user = db.Users.FirstOrDefault(x => x.UserId == id && !x.IsDeleted);

                    if (user == null)
                    {
                        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, NOT_FOUND));
                    }
                    var userDetail = new UserDto(user);

                    return(userDetail);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemplo n.º 2
0
        public ContactDto GetContactById(long id)
        {
            if (id == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, INVALID_PARAMEATER));
            }

            try
            {
                using (var db = new MishWishEntities())
                {
                    //Get selected contact
                    var contact = db.Contacts.FirstOrDefault(x => x.UserId == id);

                    if (contact == null)
                    {
                        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, USER_NOT_FOUND));
                    }

                    var contactDetail = new ContactDto(contact);

                    return(contactDetail);
                }
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, ex.Message));
            }
        }
Exemplo n.º 3
0
        public List <UserDto> GetUsers()
        {
            try
            {
                using (var db = new MishWishEntities())
                {
                    var users = db.Users.Where(us => us.IsActive).Select(u => new UserDto {
                        FirstName    = u.FirstName,
                        LastName     = u.LastName,
                        DOB          = u.DOB,
                        EmailAddress = u.Email,
                        Gender       = u.Gender,
                        MobileNumber = u.MobileNumber,
                        UserId       = u.UserId
                    }).ToList();


                    return(users);
                }
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, ex.Message));
            }
        }
Exemplo n.º 4
0
        public List <ContactDto> GetAllContacts(long userId)
        {
            if (userId == 0)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, INVALID_USERID));
            }

            // Contact list that return list.
            List <ContactDto> ContactList = new List <ContactDto>();

            try
            {
                using (var db = new MishWishEntities())
                {
                    // Get all contact list that associated give user.
                    var contacts = db.Contacts.Where(u => u.UserId == userId).Select(c => new ContactDto
                    {
                        FirstName    = c.FirstName,
                        LastName     = c.LastName,
                        MobileNumber = c.MobileNumber,
                        UserId       = c.UserId
                    }).ToList();

                    ContactList = contacts;
                }
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, ex.Message));
            }

            return(ContactList);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create new user
        /// </summary>
        /// <param name="userDto"></param>
        /// <param name="Request"></param>
        /// <returns></returns>
        public HttpResponseMessage CreateUser(UserDto userDto, HttpRequestMessage Request)
        {
            using (var db = new MishWishEntities())
            {
                // Convert user DTO to entity.
                var userEntity = userDto.ToEntity();

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

                try
                {
                    db.Users.Add(userEntity);
                    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 user DTO.
                UserDto returnUser = new UserDto(userEntity)
                {
                    Message = USER_SUCCESS_MESSAGE
                };

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, returnUser);
                return(response);
            }
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
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);
            }
        }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
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);
            }
        }
Exemplo n.º 10
0
        public HttpResponseMessage PostContact(ContactDto contactDto)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            using (var db = new MishWishEntities())
            {
                // Convert contact DTO to entity.
                var contactEntity = contactDto.ToEntity();

                try
                {
                    db.Contacts.Add(contactEntity);
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    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_MESSAGE
                };

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