Selaa lähdekoodia

Added Template, PlaceHolder and Info

master
Kobus 5 vuotta sitten
vanhempi
commit
63f404ff16

+ 10
- 0
UnivateProperties_API/Containers/Communication/TemplateDto.cs Näytä tiedosto

@@ -0,0 +1,10 @@
1
+namespace UnivateProperties_API.Containers.Communication
2
+{
3
+    public class TemplateDto
4
+    {
5
+        public int Id { get; set; }
6
+        public string Name { get; set; }
7
+        public string Subject { get; set; }
8
+        public string Body { get; set; }
9
+    }
10
+}

+ 8
- 0
UnivateProperties_API/Containers/Info/ClassDto.cs Näytä tiedosto

@@ -0,0 +1,8 @@
1
+namespace UnivateProperties_API.Containers.Info
2
+{
3
+    public class ClassDto
4
+    {
5
+        public string Name { get; set; }
6
+        public string FullName{ get; set; }
7
+    }
8
+}

+ 3
- 0
UnivateProperties_API/Context/DataContext.cs Näytä tiedosto

@@ -32,6 +32,8 @@ namespace UnivateProperties_API.Context
32 32
         public virtual DbSet<Email> Emails { get; set; }
33 33
         public virtual DbSet<SMTPAccount> Accounts { get; set; }
34 34
         public virtual DbSet<SMTPHost> Hosts { get; set; }
35
+        public virtual DbSet<Template> Templates { get; set; }
36
+        public virtual DbSet<PlaceHolder> PlaceHolders { get; set; }
35 37
         #endregion Communication
36 38
 
37 39
         #region Property
@@ -160,6 +162,7 @@ namespace UnivateProperties_API.Context
160 162
             modelBuilder.Entity<Address>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
161 163
             modelBuilder.Entity<BidItem>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
162 164
             modelBuilder.Entity<ProcessFlow>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
165
+            modelBuilder.Entity<Template>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
163 166
         }
164 167
     }
165 168
 }

+ 71
- 0
UnivateProperties_API/Controllers/Communication/TemplateController.cs Näytä tiedosto

@@ -0,0 +1,71 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Threading.Tasks;
5
+using System.Transactions;
6
+using Microsoft.AspNetCore.Http;
7
+using Microsoft.AspNetCore.Mvc;
8
+using UnivateProperties_API.Model.Communication;
9
+using UnivateProperties_API.Repository;
10
+
11
+namespace UnivateProperties_API.Controllers.Communication
12
+{
13
+    [Route("api/[controller]")]
14
+    [ApiController]
15
+    public class TemplateController : ControllerBase
16
+    {
17
+        private readonly IRepository<Template> _Repo;
18
+
19
+        public TemplateController(IRepository<Template> repo)
20
+        {
21
+            _Repo = repo;
22
+        }
23
+
24
+        [HttpGet]
25
+        public IActionResult Get()
26
+        {
27
+            var items = _Repo.GetAll();
28
+            return new OkObjectResult(items);
29
+        }
30
+
31
+        [HttpGet("{id}")]
32
+        public IActionResult Get(int id)
33
+        {
34
+            var item = _Repo.Get(x => x.Id == id);
35
+            return new OkObjectResult(item);
36
+        }
37
+
38
+        [HttpPost]
39
+        public IActionResult Post([FromBody] Template item)
40
+        {
41
+            using (var scope = new TransactionScope())
42
+            {
43
+                _Repo.Insert(item);
44
+                scope.Complete();
45
+                return CreatedAtAction(nameof(Get), new { id = item.Id }, item);
46
+            }
47
+        }       
48
+
49
+        [HttpPut("{id}")]
50
+        public IActionResult Put([FromBody] Template item)
51
+        {
52
+            if (item != null)
53
+            {
54
+                using (var scope = new TransactionScope())
55
+                {
56
+                    _Repo.Update(item);
57
+                    scope.Complete();
58
+                    return new OkResult();
59
+                }
60
+            }
61
+            return new NoContentResult();
62
+        }
63
+
64
+        [HttpDelete("{id}")]
65
+        public IActionResult Delete(int id)
66
+        {
67
+            _Repo.RemoveAtId(id);
68
+            return new OkResult();
69
+        }
70
+    }
71
+}

+ 50
- 0
UnivateProperties_API/Controllers/InfoController.cs Näytä tiedosto

@@ -0,0 +1,50 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Threading.Tasks;
5
+using Microsoft.AspNetCore.Http;
6
+using Microsoft.AspNetCore.Mvc;
7
+using UnivateProperties_API.Containers.Info;
8
+using UnivateProperties_API.Model;
9
+
10
+namespace UnivateProperties_API.Controllers
11
+{
12
+    [Route("api/[controller]")]
13
+    [ApiController]
14
+    public class InfoController : ControllerBase
15
+    {
16
+        [HttpGet]
17
+        public IActionResult Get()
18
+        {
19
+            return new OkObjectResult("hallo");
20
+        }
21
+
22
+        [HttpGet("{name}")]
23
+        public List<string> Get(string name)
24
+        {
25
+            List<string> list = new List<string>();
26
+            var newClass = System.Reflection.Assembly.GetAssembly(typeof(BaseEntity)).CreateInstance(name);
27
+            if(newClass is BaseEntity)
28
+            {
29
+                list = (newClass as BaseEntity).GetAllProperties().ToList();
30
+            }
31
+            return list;
32
+        }
33
+
34
+        [HttpGet("getAllClasses")]
35
+        public List<ClassDto> GetAllClasses()
36
+        {
37
+            List<ClassDto> list = new List<ClassDto>();
38
+            IEnumerable<BaseEntity> exporters = typeof(BaseEntity)
39
+                .Assembly.GetTypes()
40
+                .Where(t => t.IsSubclassOf(typeof(BaseEntity)) && !t.IsAbstract)
41
+                .Select(t => (BaseEntity)Activator.CreateInstance(t));
42
+            foreach(var item in exporters)
43
+            {
44
+                ClassDto cls = new ClassDto();
45
+                list.Add(new ClassDto() { FullName = item.GetType().FullName.ToString(), Name = item.GetType().Name });
46
+            }
47
+            return list;
48
+        }
49
+    }
50
+}

+ 21
- 0
UnivateProperties_API/Model/BaseEntity.cs Näytä tiedosto

@@ -1,6 +1,8 @@
1 1
 using System;
2
+using System.Collections.Generic;
2 3
 using System.ComponentModel.DataAnnotations;
3 4
 using System.ComponentModel.DataAnnotations.Schema;
5
+using System.Reflection;
4 6
 using UnivateProperties_API.Containers;
5 7
 
6 8
 namespace UnivateProperties_API.Model
@@ -39,6 +41,25 @@ namespace UnivateProperties_API.Model
39 41
             ValidateEntity v = new ValidateEntity();
40 42
             return v;
41 43
         }
44
+
45
+        public object this[string propertyName]
46
+        {
47
+            get { return GetType().GetProperty(propertyName).GetValue(this, null); }
48
+            set { GetType().GetProperty(propertyName).SetValue(this, value, null); }
49
+        }
50
+
51
+        public string[] GetAllProperties()
52
+        {
53
+            if (this == null) return new string[] { };
54
+            Type t = GetType();
55
+            PropertyInfo[] props = t.GetProperties();
56
+            List<string> propNames = new List<string>();
57
+            foreach (PropertyInfo prp in props)
58
+            {
59
+                propNames.Add(prp.Name);
60
+            }
61
+            return propNames.ToArray();
62
+        }
42 63
         #endregion Methods
43 64
     }
44 65
 }

+ 18
- 0
UnivateProperties_API/Model/Communication/PlaceHolder.cs Näytä tiedosto

@@ -0,0 +1,18 @@
1
+using System.ComponentModel.DataAnnotations;
2
+using System.ComponentModel.DataAnnotations.Schema;
3
+
4
+namespace UnivateProperties_API.Model.Communication
5
+{
6
+    public class PlaceHolder
7
+    {
8
+        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
9
+        [Key]
10
+        public int Id { get; set; }
11
+        public string Name { get; set; }
12
+        public string BoundTo { get; set; }
13
+        [ForeignKey("Template")]
14
+        public int TemplateId { get; set; }
15
+
16
+        public virtual Template Template { get; set; }
17
+    }
18
+}

+ 16
- 0
UnivateProperties_API/Model/Communication/Template.cs Näytä tiedosto

@@ -0,0 +1,16 @@
1
+using System.Collections.Generic;
2
+
3
+namespace UnivateProperties_API.Model.Communication
4
+{
5
+    public class Template : BaseEntity
6
+    {
7
+        #region Properties
8
+
9
+        public string Name { get; set; }
10
+        public string Subject { get; set; }
11
+        public string Body { get; set; }
12
+
13
+        public virtual ICollection<PlaceHolder> PlaceHolders { get; set; }
14
+        #endregion
15
+    }
16
+}

+ 8
- 0
UnivateProperties_API/Repository/Communication/EmailRepository.cs Näytä tiedosto

@@ -23,6 +23,14 @@ namespace UnivateProperties_API.Repository.Communication
23 23
 
24 24
         public List<Email> GetAll()
25 25
         {
26
+            foreach(var item in _dbContext.Emails.ToList())
27
+            {
28
+                foreach(var innerItem in item.GetAllProperties())
29
+                {
30
+                    var vv = innerItem;
31
+                }
32
+                var v = (string)item["Subject"];
33
+            }
26 34
             return _dbContext.Emails.ToList();
27 35
         }
28 36
 

+ 123
- 0
UnivateProperties_API/Repository/Communication/TemplateRepository.cs Näytä tiedosto

@@ -0,0 +1,123 @@
1
+using Microsoft.EntityFrameworkCore;
2
+using System;
3
+using System.Collections.Generic;
4
+using System.Linq;
5
+using UnivateProperties_API.Containers.Communication;
6
+using UnivateProperties_API.Context;
7
+using UnivateProperties_API.Model.Communication;
8
+
9
+namespace UnivateProperties_API.Repository.Communication
10
+{
11
+    public class TemplateRepository : IRepository<Template>
12
+    {
13
+        private readonly DataContext _dbContext;
14
+
15
+        public TemplateRepository(DataContext dbContext)
16
+        {
17
+            _dbContext = dbContext;
18
+        }
19
+
20
+        public List<Template> Get(Func<Template, bool> where)
21
+        {
22
+            return _dbContext.Templates.Where(where).ToList();
23
+        }
24
+
25
+        public List<Template> GetAll()
26
+        {
27
+            return _dbContext.Templates.ToList();
28
+        }
29
+
30
+        public Template GetDetailed(Func<Template, bool> first)
31
+        {
32
+            var item = _dbContext.Templates.FirstOrDefault(first);
33
+            return item;
34
+        }
35
+
36
+        public List<Template> GetDetailedAll()
37
+        {
38
+            var list = GetAll();
39
+            return list;
40
+        }
41
+
42
+        public void Insert(Template item)
43
+        {
44
+            item.Id = NewId();
45
+            _dbContext.Add(item);
46
+            Save();
47
+        }
48
+
49
+        public void Insert(IEnumerable<Template> items)
50
+        {
51
+            int id = NewId();
52
+            foreach (var item in items)
53
+            {
54
+                item.Id = id;
55
+                _dbContext.Add(item);
56
+                id += 1;
57
+            }
58
+            Save();
59
+        }
60
+
61
+        public void Remove(Template item)
62
+        {
63
+            var i = _dbContext.Templates.Find(item);
64
+            _dbContext.Templates.Remove(i);
65
+            Save();
66
+        }
67
+
68
+        public void Remove(IEnumerable<Template> items)
69
+        {
70
+            foreach (var item in items)
71
+            {
72
+                Template i = _dbContext.Templates.Find(item);
73
+                _dbContext.Templates.Remove(i);
74
+            }
75
+            Save();
76
+        }
77
+
78
+        public void RemoveAtId(int item)
79
+        {
80
+            var i = _dbContext.Templates.Find(item);
81
+            _dbContext.Templates.Remove(i);
82
+            Save();
83
+        }
84
+
85
+        public void Update(Template item)
86
+        {
87
+            _dbContext.Entry(item).State = EntityState.Modified;
88
+            Save();
89
+        }
90
+
91
+        public void Save()
92
+        {
93
+            _dbContext.SaveChanges();
94
+        }
95
+
96
+        public int NewId()
97
+        {
98
+            int id = 0;
99
+            if (_dbContext.Templates.Count() > 0)
100
+            {
101
+                id = _dbContext.Templates.Max(x => x.Id);
102
+            }
103
+            id += 1;
104
+            return id;
105
+        }
106
+
107
+        public List<TemplateDto> GetSimpleAll()
108
+        {
109
+            List<TemplateDto> list = new List<TemplateDto>();
110
+            foreach(var item in GetAll())
111
+            {
112
+                list.Add(new TemplateDto()
113
+                {
114
+                    Id = item.Id,
115
+                    Name = item.Name,
116
+                    Subject = item.Subject,
117
+                    Body = item.Body
118
+                });
119
+            }
120
+            return list;
121
+        }
122
+    }
123
+}

+ 1
- 0
UnivateProperties_API/Startup.cs Näytä tiedosto

@@ -131,6 +131,7 @@ namespace UnivateProperties_API
131 131
             services.AddTransient<IRepository<Individual>, IndividualRepository>();
132 132
             #endregion User
133 133
             #region Communication
134
+            services.AddTransient<IRepository<Template>, TemplateRepository>();
134 135
             services.AddTransient<IRepository<Email>, EmailRepository>();
135 136
             services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
136 137
             services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();

Loading…
Peruuta
Tallenna