using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using App.DBAccess;
using App.Model;
namespace App.UIService
{
interface ISubjectUIService
{
ActionDetails SubjectInsert(SubjectModel obj);
ActionDetails SubjectsSelect();
ActionDetails SubjectSelect(int subjectID);
ActionDetails SubjectDelete(int subjectID);
ActionDetails SubjectUpdate(SubjectModel obj);
}
public class SubjectUIService:Repository,ISubjectUIService
{
public SubjectUIService()
: base()
{
}
public ActionDetails SubjectInsert(SubjectModel obj)
{
try
{
List<string> elist = new List<string> ();
if (ValidateSubjectModel(obj, out elist)) {
return Message.NotSuccess(content: null, messageContent: Message.MessageBody("subject insert is not success", EErrorCode.ValidateionError, elist));
}
dba.TB_SUBJECTS.Add(
BoObjectsToDb(new List<SubjectModel>() { obj }).FirstOrDefault()
);
dba.SaveChanges();
return Message.Success(content:obj,messageContent:Message.MessageBody("subject insert is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
public ActionDetails SubjectsSelect()
{
try
{
var c = DBObjectToBo(dba.TB_SUBJECTS.ToList());
return Message.Success(content: c, messageContent: Message.MessageBody("subject select is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
public ActionDetails SubjectSelect(int subjectID)
{
try
{
var c = DBObjectToBo(new List<TB_SUBJECTS>() { dba.TB_SUBJECTS.Where(p => p.ID == subjectID).FirstOrDefault() }).FirstOrDefault();
return Message.Success(content: c, messageContent: Message.MessageBody("subject select is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
public ActionDetails SubjectDelete(int subjectID)
{
try
{
var o = dba.TB_SUBJECTS.SingleOrDefault(p => p.ID == subjectID);
if (o == null)
{
return Message.NotSuccess(content: null, messageContent: Message.MessageBody("invalied oparation", EErrorCode.ValidateionError));
}
dba.TB_SUBJECTS.Remove(o);
dba.SaveChanges();
return Message.Success(content: null, messageContent: Message.MessageBody("subject delete is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
public ActionDetails SubjectUpdate(SubjectModel obj)
{
try
{
List<string> elist = new List<string>();
if (ValidateSubjectModel(obj, out elist))
{
return Message.NotSuccess(content: null, messageContent: Message.MessageBody("subject update is not success", EErrorCode.ValidateionError, elist));
}
var o = dba.TB_SUBJECTS.SingleOrDefault(p => p.ID == obj.ID);
if (o == null)
{
return Message.NotSuccess(content: null, messageContent: Message.MessageBody("invalied oparation", EErrorCode.ValidateionError));
}
o.NAME = obj.Name;
o.FEES = obj.Fees;
dba.SaveChanges();
return Message.Success(content: obj, messageContent: Message.MessageBody("subject update is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
#region validate subject model
bool ValidateSubjectModel(SubjectModel obj,out List<string> errors) {
List<string> emessages = new List<string>();
bool isvalied = true;
if (string.IsNullOrEmpty(obj.Name))
{
emessages.Add("subject name requred");
isvalied = false;
}
if (obj.Fees==null)
{
emessages.Add("invalied subject fee");
isvalied = false;
}
else if (obj.Fees.Value <= 0 || obj.Fees >= 10000)
{
emessages.Add("invalied subject fee");
isvalied = false;
}
errors = emessages;
return isvalied;
}
#endregion
#region objectConverter
List<SubjectModel> DBObjectToBo(List<TB_SUBJECTS> lst)
{
if (lst == null || lst.Count == 0)
{
return new List<SubjectModel>();
}
else
{
List<SubjectModel> lstSubjects = new List<SubjectModel>();
foreach (var item in lst)
{
lstSubjects.Add(new SubjectModel
{
Fees = item.FEES,
ID = item.ID,
Name = item.NAME
});
}
return lstSubjects;
}
}
List<TB_SUBJECTS> BoObjectsToDb(List<SubjectModel> lst)
{
if (lst == null || lst.Count == 0)
{
return new List<TB_SUBJECTS>();
}
else
{
List<TB_SUBJECTS> lstSubjects = new List<TB_SUBJECTS>();
foreach (var item in lst)
{
lstSubjects.Add(new TB_SUBJECTS
{
FEES = item.Fees,
ID = item.ID,
NAME = item.Name
});
}
return lstSubjects;
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using App.DBAccess;
using App.Model;
namespace App.UIService
{
interface ISubjectUIService
{
ActionDetails SubjectInsert(SubjectModel obj);
ActionDetails SubjectsSelect();
ActionDetails SubjectSelect(int subjectID);
ActionDetails SubjectDelete(int subjectID);
ActionDetails SubjectUpdate(SubjectModel obj);
}
public class SubjectUIService:Repository,ISubjectUIService
{
public SubjectUIService()
: base()
{
}
public ActionDetails SubjectInsert(SubjectModel obj)
{
try
{
List<string> elist = new List<string> ();
if (ValidateSubjectModel(obj, out elist)) {
return Message.NotSuccess(content: null, messageContent: Message.MessageBody("subject insert is not success", EErrorCode.ValidateionError, elist));
}
dba.TB_SUBJECTS.Add(
BoObjectsToDb(new List<SubjectModel>() { obj }).FirstOrDefault()
);
dba.SaveChanges();
return Message.Success(content:obj,messageContent:Message.MessageBody("subject insert is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
public ActionDetails SubjectsSelect()
{
try
{
var c = DBObjectToBo(dba.TB_SUBJECTS.ToList());
return Message.Success(content: c, messageContent: Message.MessageBody("subject select is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
public ActionDetails SubjectSelect(int subjectID)
{
try
{
var c = DBObjectToBo(new List<TB_SUBJECTS>() { dba.TB_SUBJECTS.Where(p => p.ID == subjectID).FirstOrDefault() }).FirstOrDefault();
return Message.Success(content: c, messageContent: Message.MessageBody("subject select is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
public ActionDetails SubjectDelete(int subjectID)
{
try
{
var o = dba.TB_SUBJECTS.SingleOrDefault(p => p.ID == subjectID);
if (o == null)
{
return Message.NotSuccess(content: null, messageContent: Message.MessageBody("invalied oparation", EErrorCode.ValidateionError));
}
dba.TB_SUBJECTS.Remove(o);
dba.SaveChanges();
return Message.Success(content: null, messageContent: Message.MessageBody("subject delete is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
public ActionDetails SubjectUpdate(SubjectModel obj)
{
try
{
List<string> elist = new List<string>();
if (ValidateSubjectModel(obj, out elist))
{
return Message.NotSuccess(content: null, messageContent: Message.MessageBody("subject update is not success", EErrorCode.ValidateionError, elist));
}
var o = dba.TB_SUBJECTS.SingleOrDefault(p => p.ID == obj.ID);
if (o == null)
{
return Message.NotSuccess(content: null, messageContent: Message.MessageBody("invalied oparation", EErrorCode.ValidateionError));
}
o.NAME = obj.Name;
o.FEES = obj.Fees;
dba.SaveChanges();
return Message.Success(content: obj, messageContent: Message.MessageBody("subject update is success"));
}
catch (Exception ex)
{
return Message.Error(ex);
}
}
#region validate subject model
bool ValidateSubjectModel(SubjectModel obj,out List<string> errors) {
List<string> emessages = new List<string>();
bool isvalied = true;
if (string.IsNullOrEmpty(obj.Name))
{
emessages.Add("subject name requred");
isvalied = false;
}
if (obj.Fees==null)
{
emessages.Add("invalied subject fee");
isvalied = false;
}
else if (obj.Fees.Value <= 0 || obj.Fees >= 10000)
{
emessages.Add("invalied subject fee");
isvalied = false;
}
errors = emessages;
return isvalied;
}
#endregion
#region objectConverter
List<SubjectModel> DBObjectToBo(List<TB_SUBJECTS> lst)
{
if (lst == null || lst.Count == 0)
{
return new List<SubjectModel>();
}
else
{
List<SubjectModel> lstSubjects = new List<SubjectModel>();
foreach (var item in lst)
{
lstSubjects.Add(new SubjectModel
{
Fees = item.FEES,
ID = item.ID,
Name = item.NAME
});
}
return lstSubjects;
}
}
List<TB_SUBJECTS> BoObjectsToDb(List<SubjectModel> lst)
{
if (lst == null || lst.Count == 0)
{
return new List<TB_SUBJECTS>();
}
else
{
List<TB_SUBJECTS> lstSubjects = new List<TB_SUBJECTS>();
foreach (var item in lst)
{
lstSubjects.Add(new TB_SUBJECTS
{
FEES = item.Fees,
ID = item.ID,
NAME = item.Name
});
}
return lstSubjects;
}
}
#endregion
}
}
No comments:
Post a Comment