Ejemplo n.º 1
0
        public static bool UserSave(UserModel user)
        {
            using (var db = new PosEntities())
            {
                var dbUser = (from dbusr in db.Users where dbusr.UserID == user.UserID select dbusr).FirstOrDefault();
                if (dbUser == null)
                {
                    dbUser = new User();
                    db.Users.Add(dbUser);
                }

                Mapper.Map<UserModel, User>(user, dbUser);

                int result = db.SaveChanges();

                if (result > 0)
                    return true;
                else
                    return false;
            }
        }
Ejemplo n.º 2
0
        public async Task<HttpResponseMessage> Post()
        {
            AjaxModel<string> ajax = new AjaxModel<string>() { Success = true, Message = PosMessage.UserSaveSuccessful, Model = PosMessage.Blank };

            try
            {
                string root = HttpContext.Current.Server.MapPath("~/Data");
                var provider = new MultipartFormDataStreamProvider(root);
                await Request.Content.ReadAsMultipartAsync(provider);

                //getting the user details
                UserModel model = new UserModel();
                model.UserID = Convert.ToInt32(provider.FormData["UserID"]);
                model.FirstName = provider.FormData["FirstName"];
                model.LastName = provider.FormData["LastName"];
                model.UserName = provider.FormData["UserName"];
                model.Password = provider.FormData["Password"];
                model.PhotoUrl = provider.FormData["PhotoUrl"];
                model.PhotoUrl = model.PhotoUrl == "" ? null : model.PhotoUrl;

                //checking whether the user exists
                if (PosRepository.UserExists(model))
                {
                    ajax.Success = false;
                    ajax.Message = PosMessage.UserNameExists;
                }
                else
                {
                    if (provider.FileData.Count > 0)
                    {
                        MultipartFileData fileData = provider.FileData[0];
                        FileInfo fi = new FileInfo(fileData.LocalFileName);

                        //getting the file saving path
                        string clientFileName = fileData.Headers.ContentDisposition.FileName.Replace(@"""", "");
                        if (clientFileName != "")
                        {
                            string clientExtension = clientFileName.Substring(clientFileName.LastIndexOf('.'));
                            string serverFileName = fi.DirectoryName + @"\" + model.UserName + clientExtension;
                            model.PhotoUrl = model.UserName;

                            FileInfo fiOld = new FileInfo(serverFileName);
                            if (fiOld.Exists)
                                fiOld.Delete();
                            //if (File.Exists())
                            fi.MoveTo(serverFileName);
                        }
                        else
                        {
                            if (fi.Exists)
                                fi.Delete();
                        }
                    }

                    PosRepository.UserSave(model);
                }
            }
            catch (Exception exp)
            {
                ajax.Success = false;
                ajax.Message = exp.Message;
            }
            return Request.CreateResponse(HttpStatusCode.OK, ajax, "application/json"); ;
        }
Ejemplo n.º 3
0
 public static bool UserExists(UserModel user)
 {
     using(var db = new PosEntities())
     {
         var dbUser = (from dbusr in db.Users where dbusr.UserName.ToLower() == user.UserName.ToLower() && dbusr.UserID != user.UserID select dbusr.UserName).FirstOrDefault();
         if (dbUser == null)
             return false;
         else
             return true;
     }
 }